加载视图的布局参数被忽略 [英] Layout params of loaded view are ignored
问题描述
我正在尝试编写自己的自定义View
,但LayoutParams
有问题.
I'm trying to write my own custom View
and I have problem with LayoutParams
.
想法是扩展ViewGroup
(LinearLayout
)
public class MyView extends LinearLayout{
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context) {
super(context);
}
public void putContent(){
setOrientation(HORIZONTAL);
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 5; i++){
View view = inflater.inflate(R.layout.item, null);
TextView tv = (TextView)view.findViewById(R.id.item_text);
tv.setText("Item " + i);
addView(view);
}
}
}
如您所见,putContent
方法会扩充项目并添加到我的视图中.这是一个项目布局
As you can see putContent
method inflates an items and adds to my view. Here is an item layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<TextView android:text="TextView"
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"/>
</LinearLayout>
和主屏幕布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android_layout_weight="1"
android:text="@string/hello"
/>
<my.test.MyView
android:id="@+id/my_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android_layout_weight="1"
/>
</LinearLayout>
和活动代码
public class Start extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyView myView = (MyView)findViewById(R.id.my_view);
myView.putContent();
}
}
这是我得到的截图
所以问题是:项目根元素的属性被忽略
So problem is: attributes of item's root element is ignored
android:layout_width="match_parent"
android:layout_height="match_parent"
但结果我想得到这样的结果(当用这一行替换 addView(view);
时我得到这个结果)
But in result I want to get something like this (I get this result when replace addView(view);
with this line)
addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F));
所以问题是:如何在没有硬编码 LayoutParams 的情况下实现这个结果?感谢您的帮助!
So question is: how I can achieve this result without hard coded LayoutParams ? Thanks for any help!
更新
我还在调试模式下查看了 view
变量字段 - mLayoutParams
是 null
,当我添加膨胀 view
使用 addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F));
进入父级.
I also look at the view
variable fields in debug mode - the mLayoutParams
is null
, and became not null when I add inflated view
into parent with addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F));
.
但是刚刚加载的视图的子视图的mLayoutParams
不为空.为什么在视图膨胀时忽略 xml 布局中只有根元素的 LayoutParams?
But mLayoutParams
of child of just-loaded view is not null.
Why LayoutParams of only root element in xml layout are ignored when view inflated?
推荐答案
使用以下语句来膨胀:
View view = inflater.inflate( R.layout.item /* resource id */,
MyView.this /* parent */,
false /*attachToRoot*/);
这篇关于加载视图的布局参数被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!