9

ボタンが押されたときにボタンにスタイルを適用する方法はありますか?

style.xmlにスタイルがある場合:

<resources>
    <style name="test">
        <item name="android:textStyle">bold</item>
    </style>
</resources>

button.xmlのセレクター:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/test_pressed"
              style="@style/test"
          android:state_pressed="true"/>
    <item android:drawable="@drawable/test_focused"
          android:state_focused="true"/>
    <item android:drawable="@drawable/test_normal"/>
</selector>

では、レイアウトでbutton.xmlをどのように参照すればよいでしょうか?

<Button
        ...
        android:???="button"/>

ありがとう!

4

7 に答える 7

7

Romain Guyは、それは不可能だと示唆しています:

http://code.google.com/p/android/issues/detail?id=8941

「セレクターは、テキストの外観ではなく、ドローアブルに対してのみ機能します。現在、これを実現する計画はありません。」

于 2011-10-09T20:36:14.017 に答える
5

これは、XML ボタン定義を使用して実現できます。

次のように、ドローアブル フォルダーに XML ファイルを作成します。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

        <item android:state_pressed="true"
        android:drawable="@drawable/green_button" /> 

        <!-- <item android:state_focused="true"
        android:drawable="@drawable/button_focused" /> --> 

        <item android:drawable="@drawable/black_button" />
</selector>

ご覧のとおり、これにより、さまざまな状態に使用するさまざまなボタン イメージを定義できます (black_button、green_button なども、drawable フォルダー内の .PNG ファイルである必要があります)。

これで、layout.xml ファイルから、ボタンの背景をボタン セレクターを指すように設定できます。

<Button android:text="Play" android:id="@+id/playBtn"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff" />

Selector XML は、他の画像ファイルと同じように、ドローアブル フォルダーから参照できます。

于 2010-09-24T10:02:20.553 に答える
2

Color State List Resourceを利用できます

リンクの例:

res/color/button_text.xml に保存された XML ファイル:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

このレイアウト XML は、カラー リストをビューに適用します。

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:textColor="@color/button_text" />
于 2016-09-21T13:55:29.560 に答える
2

クリック/プレスの色でAndroidボタンを変更するには:

色の値を定義

色の値を定義するには、プロジェクトの値のディレクトリに colors.xml ファイルを作成し、以下を追加する必要があります。res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="button_pressed">#ff8a00</color>
    <color name="button_focused">#ff8a00</color>
    <color name="button_default">#1c76bb</color>
</resources>

Drawable ディレクトリに XML ファイルを作成する

drawable フォルダーに button_background.xml ファイルを作成し、ボタンの押下/クリック、フォーカス、およびデフォルトの色を追加します。button_background.xml ファイルの最終的なコードは次のようになります。res/drawable/button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@color/button_pressed"/> <!-- pressed -->
    <item android:state_focused="true"
        android:drawable="@color/button_focused"/> <!-- focused -->
    <item android:drawable="@color/button_default"/> <!-- default -->
</selector>

ボタンの追加

res/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_background"
        android:text="Click Me"
        android:textColor="#fff" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginTop="16dp"
        android:background="@drawable/button_background"
        android:text="Click Me"
        android:textColor="#fff" />

</RelativeLayout>

ソースはこちら

于 2018-04-12T08:47:27.777 に答える
0
<Button
        android:background="@drawable/button"/>
于 2010-09-16T21:59:43.043 に答える
0

セレクターを参照するには、そのボタンの背景として指定する必要があります。

<Button
     android:background="@drawable/button"
/> 

また、押されたときの太字については、私も試していませんが、セレクターでテキストスタイルについて言及できるかもしれません。

android:textStyle="bold"

これでもうまくいかない場合は、ボタンの onClick() で実行できます。

于 2010-09-24T09:48:24.940 に答える
-3

試したことはありませんが、おそらくandroid:id="button"セレクターに含めることができます。

于 2010-09-16T19:55:10.280 に答える