-1

同じ重みのボタンが 2 つあるリニア レイアウトがあります。したがって、それらは画面全体(幅)を占めます。

1番目の下の別の線形レイアウトでは、ボタンが1つしかなく、その幅は前の2つのボタンのいずれかと同じにしたいです。

gridview や tableview などを使用する以外に、簡単な方法はありますか?

私はこれを試しました:

Button one = (Button) findViewById(R.id.one);
Button two = (Button) findViewById(R.id.two);
Button three = (Button) findViewById(R.id.three);

three.setLayoutParams(new LinearLayout.LayoutParams(
        one.getLayoutParams()));

レイアウト:

<LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/first"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

        <Button
            android:id="@+id/one"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/second"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/first"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/three"
            android:layout_width="150dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

しかし、3 番目のボタンは現在非表示になっています。

ありがとうございました

4

3 に答える 3

1

2行目でこれを試してください

<LinearLayout
    android:id="@+id/second"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/first1"
    android:weightSum="2"
    android:orientation="horizontal" >

    <Button
        android:layout_weight="1"
        android:id="@+id/three"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
</LinearLayout>
于 2013-01-10T14:16:44.423 に答える
0

3 番目のボタンが非表示になる理由は、新しい LinearLayout.LayoutParams を作成 (および設定) するときに

three.setLayoutParams(new LinearLayout.LayoutParams(one.getLayoutParams()));

重みは新しい LinearLayout.LayoutParams に転送されません。

次のコードを使用して状況を解決できます。

LinearLayout.LayoutParams newlayout = new LinearLayout.LayoutParams(one.getLayoutParams());
    newlayout.weight = 1;
    three.setLayoutParams(newlayout);

または、重みを明示的に受け取る別のコンストラクター ( LinearLayout.LayoutParams (int width, int height, float weight) ) を使用できます。

LayoutParams param = new LinearLayout.LayoutParams(one.getLayoutParams().width, one.getLayoutParams().height,((LinearLayout.LayoutParams) one.getLayoutParams()).weight);
three.setLayoutParams(param);

これで 3 つも表示されるはずです。

于 2013-01-10T14:53:21.843 に答える
0

get および set width メソッドを使用するだけで、Java コードで 3 番目のボタンの幅を設定できます。

他のボタンの 1 つの幅を取得し、それを 3 番目の幅として設定します。

次に、2 つの LinearLayouts の周りにルート レイアウトがないため、3 番目のボタンは表示されません。

android:orientation="vertical" を使用して、その周りに 3 番目の「ルート」 LinearLayout を追加する必要があります。

于 2013-01-10T14:16:10.240 に答える