为什么使用LayoutInflater与xml不一样?

时间:2015-07-02 05:18:53

标签: android android-layout layout-inflater

我创建了这个布局

<LinearLayout
    android:id="@+id/list"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="20dp"
    android:layout_weight=".7" >
    <RelativeLayout
        android:layout_width="70dp"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginLeft="10dip"
            android:src="@mipmap/icon" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:src="@mipmap/delete_button" />
        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textColor="@android:color/white"
            android:singleLine="true"
            android:textSize="10sp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:layout_below="@id/icon"
            android:text="name"/>
    </RelativeLayout>
</LinearLayout>

输出

enter image description here

当我确定显示正确时,我将项目分成另一个xml文件

mainactivity.xml

<LinearLayout
    android:id="@+id/list"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="20dp"
    android:layout_weight=".7" >
</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="70dp"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="10dip"
        android:layout_marginRight="10dip"
        android:layout_marginLeft="10dip"
        android:src="@mipmap/icon" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:src="@mipmap/delete_button" />
    <TextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textColor="@android:color/white"
        android:singleLine="true"
        android:textSize="10sp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:layout_below="@id/icon"
        android:text="name"/>
</RelativeLayout>

然后在onCreate of mainactivity中添加项目

LayoutInflater inflater=LayoutInflater.from(this);
View view=inflater.inflate(R.layout.item, null, true);
list.addView(view); //(R.id.list), LinearLayout list

现在输出

enter image description here

即使我为此linearlayout添加了许多视图,仍然只能在布局中添加一个视图

LayoutInflater inflater=LayoutInflater.from(this);
View view=inflater.inflate(R.layout.item, null, true);
View view2=inflater.inflate(R.layout.other_item, null, true);
list.addView(view); //(R.id.list), LinearLayout list
list.addView(view2);

为布局添加视图的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

请改为尝试:

  @Value("${user.maxpassiveday}")
   int maxpassiveday;

说明:

View view = inflater.inflate(R.layout.item, list, false); list.addView(view); //(R.id.list), LinearLayout list 的第二个参数是要膨胀的视图的预期父级。当您将inflate()作为第二个参数传递时,膨胀的视图不会获得任何null,因为LayoutParams不知道父项最终会是什么,因此无法创建适当的{{1} (几乎每个inflate()都定义了自己的LayoutParams子类)。

当您致电ViewGroup时,LayoutParams会检查孩子是否有addView()以及他们是否属于合适的类型。如果没有,它会生成一些默认LinearLayout以在要添加的视图上设置。无论它给孩子带来什么违约都会导致意外行为。

简而言之,解决方案是在您致电LayoutParams时传递LayoutParams而不是list

相关问题