私はAndroid2.2.3に取り組んでいます。FrameLayout内にボタンがあるので、ボタンはFrameLayout全体をカバーします。
ボタンを押すと、ボタンのセレクターだけが押されたというイベントを受け取ります。ボタンがフレームの上にあるため、フレームのセレクターはこのイベントを取得しません。つまり、属性 "android:state_pressed =" true "は、フレームではなくボタンに対して発生します。
クリックイベントではなく、プレスイベントが必要です。
ボタンが押されたときにフレームのセレクターをandroid:state_pressed="true"に設定したいと思います。
使ってみました
mMuteBtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mFrameMute.setPressed(true);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
mFrameMute.setPressed(false);
}
return false;
}
});
しかし、他の作業にも必要なonClick()イベントをブロックしました。
私のmain.xmlファイルは次のとおりです。
<FrameLayout
android:id="@+id/frame_mute1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_background" >
<Button
android:id="@+id/mute_btn1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:drawableTop="@drawable/selector_button"
android:text="mute"
android:textColor="@android:color/black"
android:textSize="12sp" />
</FrameLayout>
セレクターバックグラウンド:
<item android:drawable="@drawable/bg2" android:state_pressed="true"/>
<item android:drawable="@drawable/bg3" android:state_selected="true"/>
<!-- default -->
<item android:drawable="@drawable/bg1"/>
セレクターボタン:
<item android:drawable="@drawable/image2" android:state_pressed="true"/>
<item android:drawable="@drawable/image3" android:state_selected="true"/>
<!-- default -->
<item android:drawable="@drawable/image1"/>