Layout params of loaded view are ignored
Asked Answered
C

3

39

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?

Concretize answered 13/3, 2011 at 9:5 Comment(4)
I'ts hard to comment on your code without your actual code....Downbeat
It's not really clear. I'm assuming that above is the xml you're inflating? But what is the ll in your code? Or are you adding something to the xml, and if so, what are you adding to it?Downbeat
ll created dynamically in code, i inflate given xml and put it into llConcretize
also when i pass new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F); into addView with v it works fine ! (margins are ignored in this case)Concretize
Q
127

Use following statement to inflate:

View view = inflater.inflate( R.layout.item /* resource id */,
                                         MyView.this /* parent */,
                                         false /*attachToRoot*/);
Quelpart answered 20/3, 2011 at 4:0 Comment(3)
Your comments did the trick to my brain. attacktoroot was my bane. Thanks a lot.Eulaliaeulaliah
Indeed the comments helped recognize this answer as a viable solution among dozens of other similar ones.Diapause
what to do when the parent is null? My params from xml are sitll ignored when I do wm.addViewVillain
D
0

Ok, if I understand correctly, you have the XML mentioned above in ll, and you're adding something (R.layout.item_layout) to it.

The problems I see are:

  • Your base layout (the xml that was given, loaded into ll) has match_parent, but we do not know if it even has a parent. So the behaviour is hard to guess.
  • You did not specify what the layout params are for the item you're adding (still assuming R.layout.item_layout is not the xml you've shown). So it could be expected behaviour, or even undefined (so expected that it is unexpected).

It could be there's too much code to post here, I don't know, but then you might be better off using the hierarchy viewer to check out what params the views in your tree have, so you can actually see what's going on.

Also, mind that not all Views support margin.

Even though a view can define a padding, it does not provide any support for margins. However, view groups provide such a support. Refer to ViewGroup and ViewGroup.MarginLayoutParams for further information.

Downbeat answered 13/3, 2011 at 9:31 Comment(1)
updated post with a bit more clear problem explanation, thanks for attention!Concretize
C
0

In Android "layout_" parameters refer to the the way the current View behaves in its parent layout. In this case, its parent is ll, but we can't see what that is.

As for the layout of the TextView inside the LinearLayout, since the LinearLayout is vertical, all its children automatically have their layout_height overridden by "wrap_content" so that they can be laid out one beneath the other properly.

So now the question is, what do you actually see and what do you want to see?

Culch answered 13/3, 2011 at 9:37 Comment(2)
my items aligned at left part of screen, but i want that they fills all free spaceConcretize
updated post with a bit more clear problem explanation, thanks for attention!Concretize

© 2022 - 2024 — McMap. All rights reserved.