10

私のxmlの幅に応じて高さを持つ方法はありますか?

線形レイアウトのボタンがいくつかあります。ボタンの幅はデバイスによって異なります。これは、ボタンが画面全体を横方向に占めるためです。四角いボタンが欲しいのですが、これが私の問題です。私を助けることができる人はいますか?

<Button
            android:id="@+id/b_0xa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="false"
            android:text="@string/b_0xa" />
4

7 に答える 7

0

Java コードでプログラムを使用して簡単に実行できます。

[ を使用して] ボタンの幅を動的に取得し、int x = mButton.getWidth()返された値を使用して高さを設定します [ mButton.setHeight(x)]。

PS : LayoutParams を使用して、View の高さと幅を設定することをお勧めします。したがって、setHeight() を使用する代わりに、setLayoutParams() を使用する必要があります。

mButton.setLayoutParams(new LinearLayout.LayoutParams(x, x));

于 2013-01-26T19:11:52.010 に答える
0

ここで説明したように、XML から 100% 実行したい場合は、PercentRelativeLayout を使用して次の操作を実行できます。

<android.support.percent.PercentRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        app:layout_aspectRatio="100%"
        app:layout_widthPercent="100%" />
</android.support.percent.PercentRelativeLayout>

アプリの gradle ファイルにライブラリが含まれていることを確認してください。

compile 'com.android.support:percent:XX.X.X'
于 2017-02-11T23:44:41.447 に答える
0
<Button
  android:id="@+id/random"
  android:layout_width="60dp"
  android:layout_height="60dp"
  android:layout_weight="1"
  android:text="text view" 
/>

このコードを指定するか、高さと幅を手動で変更してください

于 2020-01-28T04:01:43.987 に答える