66

アプリにがImageButtonあり、ボタンがのときに画像の色合いを変更する必要がありますpressed/focused。次のようなXMLファイルから取得するImageButtonセットがあります。src

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- pressed -->
    <item 
        android:state_pressed="true"
        android:tint="@color/black"
        android:drawable="@drawable/search"
        />

    <!-- focused -->
    <item 
        android:state_focused="true"
        android:tint="@color/black"
        android:drawable="@drawable/search"
        />

    <!-- default -->
    <item
        android:tint="@null"
        android:drawable="@drawable/search"
        />

</selector>

ImageButtonただし、を押したりフォーカスしたりしても色合いは適用されません。画像は通常どおり表示されます。黒は#000000いつものように定義されています。何か案は?

4

10 に答える 10

107

次のコードを使用して、コードで非常に簡単に色合いを変更できます。

ImageButton button = (ImageButton) this.findViewById(R.id.button_i_want_to_modify);
button.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint

それが役に立てば幸い。

JS

于 2010-08-09T19:10:18.547 に答える
23

xmlだけを使用してそれを行う方法は次のとおりです。ドローアブルフォルダにセレクターを作成します。例:touch_selector.xml

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

    <!-- State when a row is being pressed, but hasn't yet been activated (finger down) -->
    <item android:state_pressed="true" android:color="@color/semi_slate" />

    <!-- When the view is "activated".  In SINGLE_CHOICE_MODE, it flags the active row
     of a ListView -->
    <item android:state_activated="true" android:color="@color/semi_slate" />

    <!-- Default, "just hangin' out" state. -->
    <item android:color="@android:color/transparent" />
</selector>

xmlの画像ビューで、android:tint属性を上記で作成したドローアブルに設定しました。

android:tint = "@drawable/touch_selector"

コード全体は次のようになりました。

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/poster"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:tint="@drawable/touch_selector" />

これはすべてxmlのソリューションであり、プレス時またはアクティブ時にImageViewに色合いを付けます。ImageButtonについても同様のことができます

これは、APIレベル>=21でのみ機能することに注意してください。

于 2016-12-21T20:47:45.653 に答える
11

最後に、API<21の解決策を見つけました。

Button more = (Button) findViewById(R.id.more);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    more.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
} else {
    Drawable wrapDrawable = DrawableCompat.wrap(more.getBackground());
    DrawableCompat.setTint(wrapDrawable, color));
    more.setBackgroundDrawable(DrawableCompat.unwrap(wrapDrawable));
}

これが誰かが2時間を失わないように助けてくれますように!

于 2015-11-20T18:47:45.213 に答える
8

私はこれをxmlで行う方法を見つけました(少なくともapi 21以降)。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <bitmap
            android:src="@drawable/search"
            android:tint="@color/black"
            />
    </item>
    <item android:drawable="@drawable/search"/>
</selector>

ビットマップに色合いを設定することにより、タッチをインターセプトしたり、ImageViewまたはImageButtonをサブクラス化したりすることなく、xmlで同じドローアブルを再利用できます。

セレクターを作成したら、それをImageViewまたはImageButtonのsrcとして適用します。

于 2016-07-07T19:22:19.100 に答える
3
bt.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        bt.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint
                        return true; // if you want to handle the touch event
                    case MotionEvent.ACTION_UP:
                        bt.clearColorFilter(); // White Tint
                        return true; // if you want to handle the touch event
                }
                return false;
            }
        });
于 2015-04-15T06:49:51.040 に答える
1

私がやっていることは、関数setColorFilterを持つカスタムボタンを追加することです。

このように、xmlで新しいボタンを使用できます。

public class CustomButton extends Button {

public CustomButton(Context context) {
    super(context);
}

public CustomButton(Context context, AttributeSet attributes) {
    super(context, attributes);
};

@Override
public boolean onTouchEvent(MotionEvent event) {
    int maskedAction = event.getActionMasked();
    if (maskedAction == MotionEvent.ACTION_DOWN)
        getBackground().setColorFilter(Color.argb(150, 155, 155, 155), PorterDuff.Mode.DST_IN);
    else if (maskedAction == MotionEvent.ACTION_UP)
        getBackground().setColorFilter(null);
    return super.onTouchEvent(event);
}}

およびImageButtonの場合

public class CustomImageButton extends ImageButton {

public CustomImageButton(Context context) {
    super(context);
}

public CustomImageButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int maskedAction = event.getActionMasked();
    if (maskedAction == MotionEvent.ACTION_DOWN)
        setColorFilter(Color.argb(150, 155, 155, 155), PorterDuff.Mode.DST_IN);
    else if (maskedAction == MotionEvent.ACTION_UP)
        setColorFilter(null);
    return super.onTouchEvent(event);
}}
于 2013-12-24T14:03:06.860 に答える
1

XMLでこれを行う方法を知りたい人のためにここにいくつかの要求があることに気づきました。実はとても簡単です。これは、layer-list

ボタンのドローアブル(drawable / some_button.xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/some_button_highlighted" />
    <item android:drawable="@drawable/some_button_image" />
</selector>

そして、これは強調表示されたドローアブル(drawable / some_button_highlighted.xml)です

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/some_button_image"/>
    <item>
        <shape>
            <solid android:color="@color/highlighted_button_color" />
        </shape>
    </item>
</layer-list>

これで、他のxmlでこれを使用できます。

...
android:drawable="@drawable/some_button"
...

これが将来誰かに役立つことを願っています。

于 2014-06-23T19:30:07.253 に答える
1

xmlから色(色合い)を設定できます。

transparentandroid:background="@null")を設定してbackgroundから使用しますtint

<ImageButton
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"
     android:tint="@color/Amber_200"
     android:background="@null"
     android:src="@drawable/back_selector" />
于 2016-08-29T05:09:12.917 に答える
0

私は同じ問題を抱えており、この解決策を見つけました:

logoImage.setOnTouchListener { v, event ->
            if (event.action == MotionEvent.ACTION_DOWN) {
                if (logoImage.isSelected) {
                    logoImage.setColorFilter(context.getColor(R.color.blue_unselect), PorterDuff.Mode.SRC_IN)
                } else {
                    logoImage.setColorFilter(null)
                }
            } else if (event.action == MotionEvent.ACTION_UP) {
                if (!logoImage.isSelected) {
                    logoImage.setColorFilter(null)
                } else {
                    logoImage.setColorFilter(context.getColor(R.color.blue_unselect), PorterDuff.Mode.SRC_IN)
                }
            } else if (event.action == MotionEvent.ACTION_CANCEL) {
                if (root.isSelected) {
                    logoImage.setColorFilter(null)
                } else {
                    logoImage.setColorFilter(context.getColor(R.color.blue_unselect), PorterDuff.Mode.SRC_IN)
                }
            }
            false
        }

それは私のために働いています。

于 2020-10-14T08:45:19.903 に答える
-3

セレクターをImageButtonのsrcとして定義したように、Androidは、srcのタイプに一致するものであるため、ドローアブルを取得します。したがって、色合いは使用されません。

それにもかかわらず、私は同様の問題を抱えていました。私もあなたのようなセレクターを使用しようとしましたが、android:srcの代わりにImageButtonのandroid:tint値を使用しました。もちろん、セレクターにある色合いの値は省略しました。すべての状態に同じドローアブルを使用したいので、これでも問題は解決します。不思議なことに、システムが「res / color / tint_selector.xml」(実際には私のセレクターです)を整数として解析できなかったことを示すNumberFormatExceptionが毎回発生します。具体的には、私のコードは次のようになります。

これは私のセレクターで、/ res / color/tint_selector.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="#D3D3D3"/> <!-- pressed -->
   <item android:color="#ff000000"/> <!-- default -->
</selector>

そしてこれは対応するImageButtonです:

<ImageButton android:id="@+id/program_help"
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content"
     android:src="@drawable/symbol"
     android:tint="@color/tint_selector">
</ImageButton>

現在は機能していませんが、これは少し役立つかもしれません。

于 2010-08-25T17:15:27.360 に答える