加载视图布局PARAMS被忽略视图、布局、加载、PARAMS

2023-09-04 03:25:48 作者:蜜糖

我试图写我自己的自定义查看和我有问题,的LayoutParams

I'm trying to write my own custom View and I have problem with LayoutParams.

我们的想法是要延长的ViewGroup (的LinearLayout )

The idea is to extend 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>

和活动code

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(视图); 这条线)

addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F));

所以,问题是:我如何能做到这一点的结果,而不硬codeD的LayoutParams? 感谢您的帮助!

So question is: how I can achieve this result without hard coded LayoutParams ? Thanks for any help!

更新

我也看看视图在调试模式变量字段 - 的 mLayoutParams ,并成为不为空,当我加充气视图为母公司与 addView(视图,新的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?

推荐答案

使用下面的语句来抬高:

Use following statement to inflate:

View view = inflater.inflate( R.layout.item /* resource id */,
                                         MyView.this /* parent */,
                                         false /*attachToRoot*/);