为什么LayoutInflater会忽略我指定的layout_width和layout_height布局参数?

安迪格

我在使LayoutInflater能够按预期工作时遇到了很大的麻烦,其他人也一样:如何在运行时使用layoutinflator添加视图?

为什么LayoutInflater会忽略我指定的布局参数?例如,为什么不尊重我的资源XML中的layout_widthlayout_height值?

安迪格

我已经研究了这个问题,参考了LayoutInflater文档并建立了一个小样本演示项目。以下教程显示了如何使用来动态填充布局LayoutInflater

在开始之前,请先查看LayoutInflater.inflate()参数如下:

  • resource:要加载的XML布局资源的ID(例如R.layout.main_page
  • root:可选视图,它是生成的层次结构(如果attachToRoottrue的父级,或者只是一个LayoutParams为返回的层次结构的根(如果attachToRootfalse提供一组的对象
  • attachToRoot:是否应将膨胀的层次结构附加到root参数?如果为false,则root仅用于为LayoutParamsXML中的根视图创建的正确子类

  • 返回:展开的层次结构的根视图。如果根供给和attachToRoottrue,这是根; 否则,它是膨胀的XML文件的根。

现在获取示例布局和代码。

主要布局(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与多种呼叫参数一起使用

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);
    }
}

参数变化的实际结果记录在代码中。

简介:调用时LayoutInflater不指定root会导致膨胀调用,而忽略XML中的布局参数。以root不等于调用inflatenullattachRoot=true会加载布局参数,但会再次返回根对象,这会阻止对加载的对象进行进一步的布局更改(除非您可以使用来找到它findViewById())。因此,您最可能希望使用的调用约定是:

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

为了帮助解决布局问题,强烈建议使用布局检查器”

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android数据绑定layout_width和layout_height

为什么Java会忽略我的serialVersionUID?

进度条和开关中的layout_width / heigth = wrap_content是什么意思

自动布局:是什么创建名为UIView-Encapsulated-Layout-Width&Height的约束?

ListView标头忽略layout_height

缺少必需的layout_width和layout_height属性

为什么WCF会忽略我的TokenProvider?

TextView的breakStrategy =“ balanced”和layout_width =“ wrap_content”出现错误

Android Studio讨厌的layout_width和layout_height弹出窗口

升级到Gradle插件3.0时出现Lint错误:缺少layout_width或layout_height属性

为什么sql会忽略我的句子?

为什么在布局xml中将RelativeLayout的“ layout_width”设置为固定值不起作用?

您必须提供layout_height属性

android:layout_height =“是什么意思?attr / actionBarSize”

PDFTron中的CustomRelativeLayout Layout_with和layout_height不起作用

layout_width / height如何与最小/最大宽度/高度一起使用

在html中,width和hight参数会裁剪我的图片,而不是缩放图片。为什么?

Android:layout_width和height裁剪了我的图像

当布局具有android:layout_height =“ wrap_content”属性时,如何在布局底部设置元素?

线性布局内的button layout_height = wrap_content的高度比wrap_content大得多

是否可以将android:minHeight指定为与android:layout_width相同

Android Studio XML需要Layout_Height和Layout_Width

动态加载的ImageView无法获取xml文件中指定的layout_height和layout_width

错误 350 与 layout_height (Android Studio)

未知属性android:layout_width,layout_height,id,gravity,layout_gravity等xml中的属性

使用 DataBinding 时,layout_height 在布局文件中不起作用

weight=1 和 android:layout_width="0dp" 使我的视图消失

为什么不能使用 toolbarStyle 为主题中的工具栏设置 layout_height 和 layout_width?

0dp layout_width 未按预期为回收视图项中的约束布局子项工作