如果兄弟视图较大,则为MATCH_PARENT,如果兄弟视图较小,则为WRAP_CONTENT

Nae*_*mul 12 android android-layout

我在布局中有两个视图.我会打电话给他们View A,并View B分别.

????????
????????
??A??B??
????????
????????
Run Code Online (Sandbox Code Playgroud)

父布局的高度(包括View AView B)是WRAP_CONTENT.

这里的高度View BWRAP_CONTENT.也就是说,其高度可以根据其内容而改变.

我想做的是

  1. 如果内容短于内容,则将View A高度设置为View B高度.View AView B
  2. View A如果View A内容高于内容,则将高度设置为自己内容的高度View B.

所以,

①如果含量View B较高,则将View A高度设置为View B高度.

       ????????      ????????
       ????????      ????????
       ?? ?? ??      ??A?? ??
I want ??A??B??, not ?????B??.
       ?? ?? ??      ?   ? ??
       ????????      ?   ????
       ????????      ????????
Run Code Online (Sandbox Code Playgroud)

②如果含量View B较短,那么View A高度就是View A自己的高度.

       ????????      ????????
       ????????      ????????
       ?? ??B??      ??A??B??
I want ??A?????, not ????????.
       ?? ?   ?      ????????
       ????   ?
       ????????
Run Code Online (Sandbox Code Playgroud)

如果是父母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.

我怎么解决这个问题?

MH.*_*MH. 6

有不同的方法可以解决这个问题,例如创建自己的自定义ViewGroup,可能基于横向LinearLayout.但是,我认为最直接的解决方案是在运行时动态设置需求.

考虑以下布局,TextView横向只有两个s LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#f00"
    android:padding="10dp" >

    <TextView
        android:id="@+id/first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0f0"
        android:padding="5dp"
        android:text="One line" />

    <TextView
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00f"
        android:padding="5dp"
        android:text="Two\nlines"
        android:textColor="#fff" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

两者都包裹它们的高度,这基本上符合您的第一个要求.

现在要求2:出于演示目的,我已经将第二个TextView文本分成两行,以使其高于第一行.要使第一TextView场比赛达到高度,你所要做的就是:

LinearLayout container = (LinearLayout) findViewById(R.id.container);
final TextView first = (TextView) findViewById(R.id.first);
final TextView second = (TextView) findViewById(R.id.second);

container.post(new Runnable() {
    @Override public void run() {
        int h1 = first.getMeasuredHeight();
        int h2 = second.getMeasuredHeight();
        if (h1 < h2) first.getLayoutParams().height = h2;

    }
});
Run Code Online (Sandbox Code Playgroud)

'技巧'是发布到视图的队列,确保在测量子节点并具有有效高度之前逻辑不会被执行.对于要求2,如果第二个视图更高,则只需要更改第一个视图的高度,这正是run()方法中的部分所做的.