12

レイアウトに 2 つのビューがあります。View Aそれぞれと と呼びますView B

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

親レイアウト ( と を含む) の高さView AView BですWRAP_CONTENT

ここで、 の高さはView BですWRAP_CONTENT。つまり、内容に応じて高さを変更できます。

私がやりたいことは

  1. のコンテンツが のコンテンツよりも短い場合、 のView A高さを の高さに設定します。View BView AView B
  2. のコンテンツが のコンテンツよりも高いView A場合、 の高さを独自のコンテンツの高さに設定します。View AView B

そう、

① のコンテンツの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 │└─┘└─┘│.
       ││ │   │      └──────┘
       │└─┘   │
       └──────┘

親が の場合、の高さを に設定するとケース 1 にLinearLayout (Horizontal)違反し、の高さをに設定するとケース 2 に違反します。View AWRAP_CONTENTView AMATCH_PARENT

親が の場合、親の上と下の両方を揃えるRelativeLayout設定はの条件に違反します。View ARelativeLayout 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.

どうすればこの問題を解決できますか?

4

3 に答える 3

6

これにはさまざまな方法があります。たとえばViewGroup、おそらく水平方向に基づいて独自のカスタムを作成するなどLinearLayoutです。ただし、最も簡単な解決策は、実行時に要件を動的に設定することだと思います。

TextView水平に 2 つだけの次のレイアウトを検討してください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: デモンストレーションの目的で、2 番目TextViewのテキストを最初のテキストよりも高くするために 2 行に分割しました。高さの最初の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 では、最初のビューの高さを変更する必要があるのは、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 に答える