41

ImageButtonを透明に設定したので、アイコンはAndroidActionBarのような背景パネルと一致します。これは私が望むようにうまく見えます。

ただし、背景が透明の場合、アクションバーの透明なボタンを押したときのように青みがかったハイライトは表示されません。

透明で、クリックするとハイライトが点滅するImageButtonを作成できますか?

<ImageButton
    android:id="@+id/nextItemButton"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@null"
    android:src="@drawable/ic_media_ff" />
4

4 に答える 4

143

私はこれと同じ問題に遭遇しました。最後に、その属性を持つコードのサンプルを取得しました。

android:background="?android:selectableItemBackground"

この属性は、コーディングなしで任意のビュー(Button、ImageButton、TextView ...)に選択可能なハイライトを備えた透明な背景を提供します!!!

于 2012-12-13T00:45:48.987 に答える
38

あなたがする必要があるのは、適切な背景を設定することです。通常の状態では透明にし、プレス状態では青みがかった状態にしたい場合。

このようなStateListDrawableをディレクトリに作成しますres/drawable

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

このように、デフォルトの背景は透明です。押すと、背景は指定した色になります(色の代わりに、ここで描画可能な色を使用できます)。

于 2012-10-10T19:48:46.200 に答える
9

フェルナンデスの良い答えを追加するだけです:

効果を長方形ではなく円形にしたい場合は、次を使用します。

android:background="?android:selectableItemBackgroundBorderless"    

(* V21以降の場合)。

于 2015-12-16T13:59:29.903 に答える
2

プログラムで実行したい場合は、次の1つの解決策があります。

カスタムImageButtonクラスを作成してオーバーライドdrawableStateChange()

public class CustomImageButton extends ImageButton {

    @Override
    protected void drawableStateChanged() {
        Log.d("Button", "isPressed: " + isPressed() );
        if( isPressed() ){
            setBackgroundResource( android.R.color.holo_blue_dark );
        }  else {
            setBackgroundResource( android.R.color.transparent );
        }
        super.drawableStateChanged();

    }

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

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

    public CustomImageButton( Context context, AttributeSet attrs, int defStyle ) {
        super( context, attrs, defStyle );
        // TODO Auto-generated constructor stub
    }



}
于 2012-10-10T19:58:11.307 に答える