72

アプリでボタンの背景色を設定しようとしていますが、希望する結果を得ることができません...

設定しようとしている色は ですholo_green_light(#ff99cc00)。それを行うために、私は使用していますsetColorFilter(0xff99cc00, PorterDuff.Mode.MULTIPLY);

私が得た色は ではなく、holo_green_lightライトグレーと のミックスですholo_green_light

を使用してみましたが、LightingColorFilterあまり成功しませんでした。

ボタンがボタンのように表示され、必要な色の平らな長方形ではないように、プログラムでそれを行う方法はありますか?

4

11 に答える 11

18

ハードコーディングを気にしない場合は、これを行うことができます ~> android:background="#eeeeee" # 必要な16進数の色をドロップします。

このように見えます....

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:text="@string/ClickMe"
    android:background="#fff"/>
于 2014-07-17T14:04:16.663 に答える
15

/res/drawable/button.xml次の内容で作成 します。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
 <solid android:color="#90EE90"/> 
    <corners
     android:bottomRightRadius="3dp"
     android:bottomLeftRadius="3dp"
  android:topLeftRadius="3dp"
  android:topRightRadius="3dp"/>
</shape>

そして、次を使用できます:

<Button
    android:id="@+id/button_save_prefs"
    android:text="@string/save"
    android:background="@drawable/button"/>
于 2013-08-06T01:04:27.593 に答える
10

MaterialButtonaと app:backgroundTint属性を使用するだけです:

<MaterialButton
  app:backgroundTint="@color/my_color_selector"

ここに画像の説明を入力

于 2020-05-26T07:13:42.190 に答える
5

そんなに筋金入りである必要はありません。
これを試して :

YourButtonObject.setBackground(0xff99cc00);
于 2015-08-11T23:39:22.690 に答える
5

なぜ使用しないのsetBackgroundColor(getResources().getColor(R.color.holo_light_green))ですか?

編集: Android ボタンのように見えるものが必要な場合は、グラデーションを作成して背景として設定する必要があります。この例については、この質問をご覧ください。

于 2013-08-06T00:41:47.997 に答える
0

Mark Proctorの回答に加えて:

デフォルトのスタイリングを維持したいが、ボタンに条件付きの色を付けたい場合は、次のbackgroundTintようにプロパティを設定するだけです:

android:backgroundTint="@drawable/styles_mybutton"

関連ファイル /res/drawable/styles_mybutton.xml を作成し、次のテンプレートを使用して好みに応じて色を変更します。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Disabled state-->
    <item android:state_enabled="false"
        android:color="@android:color/white">
    </item>
    <!-- Default state-->
    <item
        android:color="#cfc">
    </item>
</selector>
于 2018-07-23T12:53:44.913 に答える