本文介绍了为什么LayoutInflater不理我指定的layout_width和layout_height布局参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有严重的麻烦LayoutInflater按预期方式工作,所以没有其他人:help使用layoutinflator在运行时添加的看法?。

I've had severe trouble getting LayoutInflater to work as expected, and so did other people: help for using layoutinflator to add views at runtime?.

为什么LayoutInflater不理我指定的布局参数?例如。为什么是 layout_width layout_height 从我的资源XML值不兑现?

Why does LayoutInflater ignore the layout parameters I've specified? E.g. why are the layout_width and layout_height values from my resources XML not honored?

推荐答案

我已经研究过此问题,指的是的,并设立一个小样本示范项目。以下教程演示了如何使用动态填充布局 LayoutInflater

I've investigated this issue, referring to the LayoutInflater docs and setting up a small sample demonstration project. The following tutorials shows how to dynamically populate a layout using LayoutInflater.

在我们开始之前看看 LayoutInflater.inflate()参数如下:

Before we get started see what LayoutInflater.inflate() parameters look like:


  • 资源:ID为一个XML布局资源加载(例如, R.layout.main_page

  • :可选的观点是产生层次的父(如 attachToRoot 真正),否则只是一个对象,它提供了一套的LayoutParams 值的返回层次的根(如 attachToRoot

  • attachToRoot :是否充气层次结构应连接到root参数?如果为假,根本只是用来创建的LayoutParams 的在XML根查看正确的子类。

  • resource: ID for an XML layout resource to load (e.g., R.layout.main_page)
  • root: Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
  • attachToRoot: Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

返回:充气层次的根视图。如果提供的根和 attachToRoot 真正,这是根本;否则它是膨胀的XML文件的根。

Returns: The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

现在对样品的布局和code。

Now for the sample layout and code.

主要布局(的main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</LinearLayout>

信息加入到这个容器是一个单独的TextView,小红色方块可见,如果布局参数,成功地从XML应用( red.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:background="#ff0000"
    android:text="red" />

现在 LayoutInflater 用于与调用参数几个变化

Now LayoutInflater is used with several variations of call parameters

public class InflaterTest extends Activity {

    private View view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);
      ViewGroup parent = (ViewGroup) findViewById(R.id.container);

      // result: layout_height=wrap_content layout_width=match_parent
      view = LayoutInflater.from(this).inflate(R.layout.red, null);
      parent.addView(view);

      // result: layout_height=100 layout_width=100
      view = LayoutInflater.from(this).inflate(R.layout.red, null);
      parent.addView(view, 100, 100);

      // result: layout_height=25dp layout_width=25dp
      // view=textView due to attachRoot=false
      view = LayoutInflater.from(this).inflate(R.layout.red, parent, false);
      parent.addView(view);

      // result: layout_height=25dp layout_width=25dp 
      // parent.addView not necessary as this is already done by attachRoot=true
      // view=root due to parent supplied as hierarchy root and attachRoot=true
      view = LayoutInflater.from(this).inflate(R.layout.red, parent, true);
    }
}

参数变化的实际结果都记录在code。

The actual results of the parameter variations are documented in the code.

简介:电话 LayoutInflater 不指定根导致膨胀通话忽略从XML布局参数。调用具有根不等于抬高 attachRoot = TRUE 并加载布局参数,但再次返回根对象,其中prevents到加载的对象进一步布局更改(除非你可以用它找到 findViewById())。
因此,你最有可能想使用调用约定是这样的:

SYNOPSIS: Calling LayoutInflater without specifying root leads to inflate call ignoring the layout parameters from the XML. Calling inflate with root not equal null and attachRoot=true does load the layout parameters, but returns the root object again, which prevents further layout changes to the loaded object (unless you can find it using findViewById()).The calling convention you most likely would like to use is therefore this one:

loadedView = LayoutInflater.from(context)
                .inflate(R.layout.layout_to_load, parent, false);

要帮助布局的问题,href=\"http://developer.android.com/guide/developing/tool​​s/hierarchy-viewer.html\">层次观众是强烈建议

To help with layout issues, the hierarchy viewer is highly recommended.

这篇关于为什么LayoutInflater不理我指定的layout_width和layout_height布局参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 12:09