Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

android - Layout params of loaded view are ignored

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

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);
        }
    }
}

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>

And main screen 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: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>

And activity 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();
    }
}

Here is screenshot of what i get

enter image description here

So problem is: attributes of item's root element is ignored

android:layout_width="match_parent"
android:layout_height="match_parent"

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));

enter image description here

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

update

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));. enter image description here

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?

question from:https://stackoverflow.com/questions/5288435/layout-params-of-loaded-view-are-ignored

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use following statement to inflate:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...