我想知道如何LayoutParams
将致力于LayoutInflator
。两者之间有什么区别:
LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, null); //FIRST WAY
LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, container,false); //SECOND WAY
因为,这两种方法给了我不同的结果。
实际上,第二个 inflate 方法为我提供了两个子布局更改的正确结果,但第一个方法会给我不同的结果。
这是我的代码:
MainActivity.Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout mainLayout=(LinearLayout)findViewById(R.id.mainLayout);
LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
for(int i=0;i<10;i++){
LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, null); //First WAY
// LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, mainLayout,false); //SECOND WAY
mainLayout.addView(childLayout);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
childitemlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp"
android:orientation="vertical"
android:background="#525f67">
<TextView android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button"
android:gravity="center"
/>
</LinearLayout> <!-- Both ways gives different result -->
<!--
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#525f67">
<TextView android:id="@+id/btn"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="Button"
android:gravity="center"
/>
</LinearLayout> Both method gives SAME result -->
两者之间的主要区别inflate()
方法是第二个参数(ViewGroup
参数)及其在设置正确的LayoutParams
对于膨胀布局文件的根视图。这很重要,因为LayoutParams
保留视图的各种布局属性(如宽度、高度、定位规则等),并且是该视图的父视图可以正确显示视图所必需的。
第一种方法基本上说:从该布局文件构建层次结构视图但不分配LayoutParams
到膨胀层次结构的根(可能是因为父级还不知道),也不要将膨胀视图附加到父级.
第二种充气方法说:从这个布局文件构建层次结构视图也分配适当的LayoutParams
(基于赋予 inflate 方法的第二个参数)到膨胀层次结构的根,也不要将膨胀视图附加到父级.
在第一种情况下,膨胀布局文件的根(R.layout.childitemlayout
)不会有任何LayoutParams
设置在其上(inflate
方法没有分配任何,因为第二个参数是null
它不知道哪种类型LayoutParams
生成),因此您的固定宽度/高度值会丢失。稍后当你会做的时候mainLayout.addView(childLayout);
the mainLayout
将检查LayoutParams
of the childLayout
,看看那些是null
并会自动设置一个实例LayoutParams
(使用其generateDefaultLayoutParams()
方法)。该方法在水平方向的特殊情况下LinearLayout
,将返回一个实例LayoutParams
其中宽度/高度将设置为WRAP_CONTENT
。所以你的childLayout
最终会是WRAP_CONTENT
作为其大小而不是您为其设置的固定值。
在第二种情况下,inflate
方法看到你建议了LinearLayout
mainLayout
as the ViewGroup
用于生成LayoutParams
。这意味着从布局文件中检索的固定值(用于宽度/高度)可以存储在适当的实例中LayoutParams
。当你会做的时候mainLayout.addView(childLayout);
, mainLayout
会看到childLayout
有适当的LayoutParams
实例(具有布局文件中使用的值)并且不调用它的generateDefaultLayoutParams()
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)