【发布时间】:2017-08-02 14:11:07
【问题描述】:
我在一个布局中有两个视图。
我将分别称他们为View A 和View B。
┌──────┐
│┌─┐┌─┐│
││A││B││
│└─┘└─┘│
└──────┘
父布局的高度(包括View A和View B)是WRAP_CONTENT。
这里,View B 的高度是WRAP_CONTENT。也就是说,它的高度可以根据其内容进行更改。
我想做的是
- 如果
View A的内容短于View B的内容,则将View A的高度设置为View B的高度。 - 如果
View A的内容高于View B的内容,则将View A的高度设置为其自身内容的高度。
所以,
①如果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.
我该如何解决这个问题?
【问题讨论】: