2016-01-27 10 views
9

属性を使用して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レイアウトです。

enter image description here

しかし、私はこの

<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" /> 

のようにキャンセルボタンの上に色を設定すると、ボタンは、私は幅または高さを大きくしなかったにもかかわらず、デフォルトのサイズよりも大きくなります。

キャンセルボタンが大きくなった上で、あなたが見ることができるようにこれは、スクリーンショット

です。実際に私は背景色を設定しています。背景色を設定しても、デフォルトのサイズになるように修正する方法はありますか?

enter image description here

答えて

7

:ようDPでのサイズを指定するボタンの値を設定します。ボタンを表示する。

アンドロイドのデフォルトボタンにはドロウアブルに適用されているパディングがあります。小さなもののようです。実際には同じサイズです。

あなたがあなた自身の描画可能なを作り、パディングを適用しない場合は、背景が、それは大きな思える作り、ビューの全体境界内に描画されます。

いつでも既定のボタンを使用できますが、AppCompatを使用して独自の色を適用できます。これにより、デフォルトボタンの背景の色付けがサポートされます。

<android.support.v7.widget.AppCompatButton 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:backgroundTint="#ffaa00"/> <!-- <--- Your color here --> 

...これは、同じappearenceを使用できるようになります。

+2

@WaiYanHeinはい、上で説明した理由のためです。あなたは**背景**を* ColorDrawableに置き換えます。 'backgroundTint'を使って色を変更するか、カスタムドロアブルを作成していくつかのパディングを適用するだけです。 –

+0

残念です。出来た。ありがとう。既存のJavaコードをイベントブレークしません。右 ? –

+0

@WayYanHeinもしあなたがAppCompatを使用するなら、それは正常に動作します。これはアプリの互換性のためです –

0

あなたはwidhtと高さのラップコンテンツを使用しているためです。

いくつかの選択肢があります。すばやく簡単な方法?あなたがButtonの大きさ、幅と高さ、あなたがクリックすることができる全領域、および使用画像を区別する必要が

<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="200dp" 
     android:layout_height="200dp" /> 

    <Button 
     android:background="@color/white" 
     android:layout_gravity="center_horizontal" 
     android:text="Edit" 
     android:id="@+id/btn_row_option_edit" 
     android:layout_width="200dp" 
     android:layout_height="200dp" /> 

    <Button 
     android:layout_gravity="center_horizontal" 
     android:text="Delete" 
     android:id="@+id/btn_row_option_delete" 
     android:background="@color/red" 
     android:layout_width="200dp" 
     android:layout_height="200dp" /> 

    <Button 
     android:layout_gravity="center_horizontal" 
     android:text="Cancel" 
     android:id="@+id/btn_row_option_cancel" 
     android:layout_width="100dp" 
     android:layout_height="100dp" /> 
</LinearLayout> 
+0

幅と高さの定義は唯一の選択肢だと思います。私はパディングを設定し、動作しません。私はAppCompatButtonを使用していました。 –

+0

リニアレイアウトの代わりに相対レイアウトを使用することもできます –

+0

しかし、このソリューションは、ボタン内のテキスト値がわからない場合は役に立ちません。これは、通常、あなたがアプリを翻訳するときに当てはまります。このソリューションは理想的ではありません。 –

4

<Button>タグのandroid:backgroundTint属性を使用してください。

同様:

android:backgroundTint="#6567dc"

<android.support.v7.widget.AppCompatButton>ボタンを使用する必要はありません。

+0

私は同じ問題を抱えていますが、backgroundTintを使用することができないように、デフォルトと同様にandroid:itemPressed項目を持つXMLファイルにバックグラウンドを設定する必要があります。それでは、これ以上の提案はありますか? –

+0

私はurの問題を解決しようとしています。私は解決策を得ました。 XMLでは、タグ内にを追加してください。ユーザーの色は、urアプリケーション画面の背景色にする必要があります。このように:

関連する問題