Layout params of loaded view are ignored

后端 未结 3 1999
滥情空心
滥情空心 2020-12-08 09:44

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

The idea is to extend ViewGroup (LinearLa

相关标签:
3条回答
  • 2020-12-08 10:12

    Use following statement to inflate:

    View view = inflater.inflate( R.layout.item /* resource id */,
                                             MyView.this /* parent */,
                                             false /*attachToRoot*/);
    
    0 讨论(0)
  • 2020-12-08 10:18

    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.

    0 讨论(0)
  • 2020-12-08 10:19

    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?

    0 讨论(0)
提交回复
热议问题