我在布局中有两个视图。
我会打电话给他们View A
and View B
分别。
┌──────┐
│┌─┐┌─┐│
││A││B││
│└─┘└─┘│
└──────┘
父布局的高度(包括View A
and View B
) is WRAP_CONTENT
.
这里,高度为View B
is WRAP_CONTENT
。也就是说,它的高度可以根据其内容而改变。
我想做的是
- Set
View A
的高度为View B
的高度如果View A
的内容短于View B
的内容。
- Set
View A
的高度到它自己内容的高度如果View A
的内容高于View B
的内容。
So,
① 如果内容View B
更高,那么View A
的高度设置为View B
的身高。
┌──────┐ ┌──────┐
│┌─┐┌─┐│ │┌─┐┌─┐│
││ ││ ││ ││A││ ││
I want ││A││B││, not │└─┘│B││.
││ ││ ││ │ │ ││
│└─┘└─┘│ │ └─┘│
└──────┘ └──────┘
② 如果内容View B
更短,那么View A
的高度是View A
自身内容的高度。
┌──────┐ ┌──────┐
│┌─┐┌─┐│ │┌─┐┌─┐│
││ ││B││ ││A││B││
I want ││A│└─┘│, not │└─┘└─┘│.
││ │ │ └──────┘
│└─┘ │
└──────┘
如果父母是LinearLayout (Horizontal)
, 环境View A
的高度为WRAP_CONTENT
违反情况1,并且设置View A
的高度为MATCH_PARENT
违反情况2。
如果父母是RelativeLayout
, 环境View A
对齐其父级的顶部和底部违规RelativeLayout
的条件:
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.
我怎么解决这个问题?