12

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

  1. Set View A's height to View B's height if View A's content is shorter than View B's content.
  2. Set View A's height to its own content's height if View A's content is taller than View 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?

4

3 回答 3

6

有不同的方法可以解决这个问题,例如通过创建自己的 custom 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>

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

现在要求 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;

    }
});

“诀窍”是发布到视图的队列,确保在测量孩子并具有有效高度之前不会执行逻辑。对于需求 2,只有在第二个视图更高时才需要更改第一个视图的高度,这正是run()方法中的部分所做的。

于 2013-06-15T02:55:51.030 回答
1

您需要使用布局参数以编程方式设置视图的尺寸。假设您使用 LinearLayout 来保存视图,您将执行以下操作:

//the views are called a and b
LinearLayout.LayoutParams aParams = a.getLayoutParams();
LinearLayout.LayoutParams bParams = b.getLayoutParams();
int aWidth = aParams.width;
int aHeight = aParams.height;
int bWidth = bParams.width;
int bHeight = bParams.height;
if (aHeight >= bHeight) {
    // do nothing
}
else {
    a.setLayoutParams(new LinearLayout.LayoutParams(aWidth, bHeight));
}

当您设置它们的高度时,最初将它们保留为 xml 中的 wrap_content,在此修改之前。

于 2013-06-15T02:12:52.877 回答
1

这可以很容易地解决,只需 axml.xml 即可。

保持父布局的高度为 wrap_content 并将每个子视图的高度设置为 match_parent。这将导致父布局的高度包裹到最高的孩子,并且每个子视图的高度都延伸到父母。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="text" />
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:text="More\ntext" />
</LinearLayout>
于 2017-08-02T06:11:07.250 回答