MATCH_PARENT如果兄弟姐妹的观点是较大的,如果WRAP_CONTENT兄弟的看法是小 [英] MATCH_PARENT if sibling view is larger, WRAP_CONTENT if sibling view is smaller

查看:200
本文介绍了MATCH_PARENT如果兄弟姐妹的观点是较大的,如果WRAP_CONTENT兄弟的看法是小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局两种观点。
我会打电话给他们查看A 视图B 分别。

I have two views in a layout. I'll call them View A and View B respectively.

┌──────┐
│┌─┐┌─┐│
││A││B││
│└─┘└─┘│
└──────┘

父布局的高度(包括查看A 视图B )是 WRAP_CONTENT

在这里,的高度视图B WRAP_CONTENT 。也就是说,它的高度可以相对于它的内容被改变。

Here, the height of View B is WRAP_CONTENT. That is, its height can be changed with respect to its content.

我想要做的就是


  1. 设置查看A 的高度视图B 的身高,如果的含量比视图B 更短的内容。

  2. 设置查看A 的高度,以自己的内容的身高,如果查看A 的含量比<高code>视图B 的内容。

  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.

因此​​,

①如果视图B 的含量越高,则查看A 的高度设置为视图B 的高度。

① 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││.
       ││ ││ ││      │   │ ││
       │└─┘└─┘│      │   └─┘│
       └──────┘      └──────┘

②如果视图B 的内容越短,则查看A 的高度查看A 的自己conent的高度。

② 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 │└─┘└─┘│.
       ││ │   │      └──────┘
       │└─┘   │
       └──────┘

如果父母是的LinearLayout(水平),设置查看A 的高度 WRAP_CONTENT 违反案件1和的高度设置查看A MATCH_PARENT 违反情况2。

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.

如果父母是 RelativeLayout的,设置查看A 来调整其父母的顶部和底部都违反了 RelativeLayout的的条件:
需要注意的是,你不能有RelativeLayout的大小和它的孩子们的位置之间循环依赖。例如,你不能有一个RelativeLayout的,其高度设置为WRAP_CONTENT和一个孩子设置为ALIGN_PARENT_BOTTOM。

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?

推荐答案

有通过创建自己的自定义是不同的方式去了解这一点,例如的ViewGroup ,可能是基于水平的LinearLayout 。然而,我侦察,最简单的解决办法是在运行时动态地设置的要求。

There's different ways to go about this, for example by creating your own custom ViewGroup, probably based on a horizontal LinearLayout. However, I recon that the most straightforward solution would be to dynamically set the requirements at runtime.

考虑只有两个的TextView S中的以下布局在水平的LinearLayout

Consider the following layout with just two TextViews in a horizontal 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>

这两个包裹自己的身高,这基本上符合了你的第一个要求。

Both wrap their height, which basically matches up to your first requirement.

现在要求2:演示的目的,我已经打破了第二个的TextView 的文成两行,以使其比第一个高。为了使第一的TextView 匹配它的高度,所有你需要做的是这样的:

Now requirement 2: for demonstration purposes, I've broken the second TextView's text up into two lines, in order to make it taller than the first one. To make the first TextView match it's height, all you have to do is this:

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,第一种观点的高度将只需要改变,如果第二种观点是更高,而这正是在运行部分()方法做了什么。

这篇关于MATCH_PARENT如果兄弟姐妹的观点是较大的,如果WRAP_CONTENT兄弟的看法是小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆