属性を使用してxmlに背景を設定すると、Button
ビューに問題が発生しました。背景を設定する前に、ボタンのデフォルトサイズが設定されています。しかし、私がバックグラウンドとして色を適用すると、異なるwidth
またはheight
を設定していなくても、大きくなっています。バックグラウンドがアンドロイドに設定されているときにボタンが大きくなる理由
私は
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:background="@color/white" android:layout_gravity="center_horizontal" android:id="@+id/btn_row_option_done" android:text="Done" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:background="@color/white" android:layout_gravity="center_horizontal" android:text="Edit" android:id="@+id/btn_row_option_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_gravity="center_horizontal" android:text="Delete" android:id="@+id/btn_row_option_delete" android:background="@color/red" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_gravity="center_horizontal" android:text="Cancel" android:id="@+id/btn_row_option_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
にbackground属性を設定する前に、これは私のxmlレイアウトです。ので、キャンセルボタンはスクリーンショットのようなデフォルトサイズはボタン
しかし、私はこの
<Button
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:text="Cancel"
android:id="@+id/btn_row_option_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
のようにキャンセルボタンの上に色を設定すると、ボタンは、私は幅または高さを大きくしなかったにもかかわらず、デフォルトのサイズよりも大きくなります。
キャンセルボタンが大きくなった上で、あなたが見ることができるようにこれは、スクリーンショット
です。実際に私は背景色を設定しています。背景色を設定しても、デフォルトのサイズになるように修正する方法はありますか?
@WaiYanHeinはい、上で説明した理由のためです。あなたは**背景**を* ColorDrawableに置き換えます。 'backgroundTint'を使って色を変更するか、カスタムドロアブルを作成していくつかのパディングを適用するだけです。 –
残念です。出来た。ありがとう。既存のJavaコードをイベントブレークしません。右 ? –
@WayYanHeinもしあなたがAppCompatを使用するなら、それは正常に動作します。これはアプリの互換性のためです –