layout_weight 忽略,如果以编程方式添加视图
layout_weight ignored, if view is added programmatically
我有一个视图,其中我有两个水平对齐的项目来教其他项目,占据父级的整个宽度 (50:50):
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/selection_container">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#0c2003">
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#Ac2003">
</LinearLayout>
</LinearLayout>
它按预期工作:
问题是当我尝试以编程方式将这些项目 (item.xml) 添加到父项 (container.xml) 时,layout_weight 属性 似乎被忽略了一些理由。而不是项目 filling/expanding 全宽,每个项目占用 space:
的几个像素
我正在通过以下方式以编程方式添加视图:
LinearLayout container = mainView.findViewById( R.id.container );
View item1 = LayoutInflater.from( mainView.getContext() ).inflate( R.layout.item, null, false );
View item2 = LayoutInflater.from( mainView.getContext() ).inflate( R.layout.item, null, false );
container.addView( item1 );
container.addView( item2 );
之后我试着打电话:
container.invalidate();
container.requestLayout();
但这没有任何区别。
container.xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/selection_container"/>
item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#0c2003">
</LinearLayout>
为什么在以编程方式添加视图时忽略 layout_weight 属性?谢谢!
问题是我没有为膨胀函数提供 'parent' 参数。如果未提供参数,则会忽略某些属性,例如 width/height/weight,因为充气机需要知道其父级是谁才能正确布置充气视图。
View item1 = LayoutInflater.from( mainView.getContext() ).inflate( R.layout.item, (ViewGroup)itemView, false );
我有一个视图,其中我有两个水平对齐的项目来教其他项目,占据父级的整个宽度 (50:50):
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/selection_container">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#0c2003">
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#Ac2003">
</LinearLayout>
</LinearLayout>
它按预期工作:
问题是当我尝试以编程方式将这些项目 (item.xml) 添加到父项 (container.xml) 时,layout_weight 属性 似乎被忽略了一些理由。而不是项目 filling/expanding 全宽,每个项目占用 space:
的几个像素我正在通过以下方式以编程方式添加视图:
LinearLayout container = mainView.findViewById( R.id.container );
View item1 = LayoutInflater.from( mainView.getContext() ).inflate( R.layout.item, null, false );
View item2 = LayoutInflater.from( mainView.getContext() ).inflate( R.layout.item, null, false );
container.addView( item1 );
container.addView( item2 );
之后我试着打电话:
container.invalidate();
container.requestLayout();
但这没有任何区别。
container.xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/selection_container"/>
item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#0c2003">
</LinearLayout>
为什么在以编程方式添加视图时忽略 layout_weight 属性?谢谢!
问题是我没有为膨胀函数提供 'parent' 参数。如果未提供参数,则会忽略某些属性,例如 width/height/weight,因为充气机需要知道其父级是谁才能正确布置充气视图。
View item1 = LayoutInflater.from( mainView.getContext() ).inflate( R.layout.item, (ViewGroup)itemView, false );