0

私はこのシナリオを持っています:コンテナとを埋める水平LinearLayoutとweightSum=100、それぞれ50の重みを持つ内部の2つのビュー。

次に、これら2つのビューを正方形にするにはどうすればよいですか(たとえば、高さはそれらの幅と等しくなければなりません)。LinearLayoutの行数は不明であるため、基本的に、この場合、重み付きの垂直コンテナーにそれらをラップすることはできません。

4

1 に答える 1

1

では、これら 2 つのビューを正方形にするにはどうすればよいでしょうか (たとえば、高さは幅と等しくなければなりません)。

にこれら 2 つのビューしかない場合は、次のLinearLayout2 つのオプションがあります。

  1. レイアウト ファイルをコンテンツ ビューとして直接設定しないでください。代わりに、そのファイルからルートへの参照を持つようにレイアウト ファイルをインフレートしてください。次に、そのルート ビューに を投稿し、目的の高さを計算して、それらをラップするRunnableの 2 つの子のそれぞれに戻します。LinearLayout

    final View v = getLayoutInflater().inflate(
            R.layout.views_specialheight, null);
    v.post(new Runnable() {
    
        @Override
        public void run() {
            setupHeight((ViewGroup) v);
        }
    });
    setContentView(v);
    

    メソッドはどこにsetupHeight()ありますか:

    private void setupHeight(ViewGroup vg) {
        int count = vg.getChildCount();
        for (int i = 0; i < count; i++) {
            final View v = vg.getChildAt(i);            
            if (v instanceof LinearLayout) {
                int width = v.getWidth();
                LinearLayout.LayoutParams lp;
                View one = ((LinearLayout) v).getChildAt(0);
                lp = (LinearLayout.LayoutParams) one.getLayoutParams();
                lp.height = width / 2;
                one.setLayoutParams(lp);
                View two = ((LinearLayout) v).getChildAt(1);
                lp = (LinearLayout.LayoutParams) two.getLayoutParams();
                lp.height = width / 2;
                two.setLayoutParams(lp);
            }
        }
    }
    

    これらの行ViewGroupをラップするラッパー サブクラスがある場合、このメソッドは非常にうまく機能します。LinearLayoutそれは改善することができます(そして改善する必要があります)が、あなたは要点を理解していると確信しています.

  2. 2番目のオプションは、次のような代わりにカスタムを使用することですViewGroup(これは、で何をするつもりだったかによって使用される場合があります) :LinearLayoutLinearLayout

    <com.luksprog.oat.views.SpecialHeightViewGroup
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >
    
       <Button
           android:id="@+id/button1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="#0077cc"
           android:text="Button" />
    
       <Button
           android:id="@+id/button2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="#99cc00"
           android:text="Button" />
    </com.luksprog.oat.views.SpecialHeightViewGroup>
    

は次のSpecialHeightViewGroupようなクラスです。

class SpecialHeightViewGroup extends ViewGroup {

public SpecialHeightViewGroup(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
}

public SpecialHeightViewGroup(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SpecialHeightViewGroup(Context context) {
    super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    if (widthMode == MeasureSpec.EXACTLY
            || widthMode == MeasureSpec.AT_MOST) {
        measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
                MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                widthSize / 2, MeasureSpec.EXACTLY));
        measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
                MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                widthSize / 2, MeasureSpec.EXACTLY));
        setMeasuredDimension(widthSize, widthSize / 2);
    } else {
        widthSize = 800; // we don't have restrictions, make it as big as we want
        measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
                MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                widthSize / 2, MeasureSpec.EXACTLY));
        measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
                MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                widthSize / 2, MeasureSpec.EXACTLY));
        setMeasuredDimension(widthSize, 400);
    }       
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {      
    View one = getChildAt(0);
    one.layout(0, 0, getWidth() / 2, getHeight());
    View two = getChildAt(1);
    two.layout(getWidth() / 2, 0, getWidth(), getHeight());     
}

}

カスタム クラスは適切にテストされていないため、バグがある可能性があります (これは単なる例です)。

オフトピック: ひょっとして、新しい Windows Phone のような UI を作成しようとしているのですか (タイルを使用)?

于 2012-09-20T15:13:54.943 に答える