I have two views in a layout.
I'll call them View A
and View B
respectively.
┌──────┐
│┌─┐┌─┐│
││A││B││
│└─┘└─┘│
└──────┘
The height of parent layout (which includes View A
and View B
) is WRAP_CONTENT
.
Here, the height of View B
is WRAP_CONTENT
. That is, its height can be changed with respect to its content.
What I want to do is
- Set
View A
's height toView B
's height ifView A
's content is shorter thanView B
's content. - Set
View A
's height to its own content's height ifView A
's content is taller thanView B
's content.
So,
① If content of View B
is taller, then View A
's height is set to View B
's height.
┌──────┐ ┌──────┐
│┌─┐┌─┐│ │┌─┐┌─┐│
││ ││ ││ ││A││ ││
I want ││A││B││, not │└─┘│B││.
││ ││ ││ │ │ ││
│└─┘└─┘│ │ └─┘│
└──────┘ └──────┘
② If content of View B
is shorter, then View A
's height is View A
's own conent's height.
┌──────┐ ┌──────┐
│┌─┐┌─┐│ │┌─┐┌─┐│
││ ││B││ ││A││B││
I want ││A│└─┘│, not │└─┘└─┘│.
││ │ │ └──────┘
│└─┘ │
└──────┘
If the parent is LinearLayout (Horizontal)
, setting View A
's height to WRAP_CONTENT
violates case 1, and setting View A
's height to MATCH_PARENT
violates case 2.
If the parent is RelativeLayout
, setting View A
to align both its parent's top and bottom violates RelativeLayout
's condition:
Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.
How can I solve this problem?