127

これは、通常は赤く表示され、押されると灰色に表示されるボタン セレクターです。

押されたときにテキストのサイズと色も変更できるように、コードをさらに直接変更するにはどうすればよいですか? どうもありがとう!

<item android:state_pressed="true" >         
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="2dp" android:color="@color/black" />
        <solid android:color="@color/grey"/>
        <padding android:left="5dp" android:top="2dp" 
            android:right="5dp" android:bottom="2dp" /> 
        <corners android:radius="5dp" /> 
    </shape>    
</item>

<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="2dp" android:color="@color/black" />
        <solid android:color="#FF6699"/>
        <padding android:left="5dp" android:top="2dp" 
            android:right="5dp" android:bottom="2dp" /> 
        <corners android:radius="5dp" /> 
    </shape>
</item>

4

6 に答える 6

231

レイアウト ファイルでselectorを設定するだけです。button

<Button
     android:id="@+id/button1"
     android:background="@drawable/selector_xml_name"
     android:layout_width="200dp"
     android:layout_height="126dp"
     android:text="Hello" />

そして完了。

編集

以下はディレクトリbutton_effect.xml内のファイルですdrawable

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

    <item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_normal"></item>

</selector>

これには、3 つのドローアブルがあることがわかります。上で書いたように、このbutton_effectスタイルをに配置するだけです。に置き換えるbuttonだけです。selector_xml_namebutton_effect

于 2012-12-24T17:11:44.040 に答える
30

state list drawableでテキストサイズを変更することはできません。テキストの色とテキストのサイズを変更するには、次の操作を行います。

テキストの色

テキストの色を変更するには、 color state list resourceを作成できます。res/color/ディレクトリにある別のリソースになります。レイアウト xml では、属性の値として設定する必要がありandroid:textColorます。カラーセレクターには、次のようなものが含まれます。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/text_pressed" />
    <item android:color="@color/text_normal" />
</selector>

文字サイズ

リソースだけでテキストのサイズを変更することはできません。「次元セレクター」はありません。コードで行う必要があります。そして、簡単な解決策はありません。

おそらく最も簡単な解決策はView.onTouchListener()、アップイベントとダウンイベントを利用してそれに応じて処理することです。次のようなものを使用します。

view.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // change text size to the "pressed value"
                return true;
            case MotionEvent.ACTION_UP:
                // change text size to the "normal value"
                return true;
            default:
                return false;
            }
        }
});

別の解決策は、ビューを拡張してメソッドをオーバーライドするsetPressed(Boolean)ことです。このメソッドは、押された状態の変化が発生したときに内部的に呼び出されます。次に、メソッド呼び出しでそれに応じてテキストのサイズを変更します (スーパーを呼び出すことを忘れないでください)。

于 2012-12-24T17:47:37.020 に答える
18

drawable フォルダーに custom_selector.xml を作成する

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/unselected" android:state_pressed="true" />
   <item android:drawable="@drawable/selected" />
</selector>

ドローアブル フォルダーに selected.xml シェイプを作成する

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
   <solid android:color="@color/selected"/>
   <padding />
   <stroke android:color="#000" android:width="1dp"/>
   <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
</shape>

ドローアブル フォルダーに unselected.xml シェイプを作成する

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
   <solid android:color="@color/unselected"/>
   <padding />
   <stroke android:color="#000" android:width="1dp"/>
   <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
</shape>

値フォルダーの color.xml に、選択/非選択状態の次の色を追加します。

<color name="selected">#a8cf45</color>
<color name="unselected">#ff8cae3b</color>

ここから完全なソリューションを確認できます

于 2015-10-14T05:46:18.183 に答える
2

次のコードを使用できます。

<Button
    android:id="@+id/img_sublist_carat"
    android:layout_width="70dp"
    android:layout_height="68dp"
    android:layout_centerVertical="true"
    android:layout_marginLeft="625dp"
    android:contentDescription=""
    android:background="@drawable/img_sublist_carat_selector"
    android:visibility="visible" />

(セレクターファイル) img_sublist_carat_selector.xml:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_focused="true" 
       android:state_pressed="true"        
       android:drawable="@drawable/img_sublist_carat_highlight" />
 <item android:state_pressed="true" 
       android:drawable="@drawable/img_sublist_carat_highlight" />
 <item android:drawable="@drawable/img_sublist_carat_normal" />
</selector>
于 2015-07-09T10:12:35.523 に答える