单击任何子视图时,父视图的选择器不起作用

Selector of the parent view is not working when any child is clicked

选择器的

XML是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/primary_light_transparent" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@color/primary_light_transparent" android:state_enabled="true" android:state_focused="true"/>
    <item android:drawable="@color/primary_light_transparent" android:state_enabled="true" android:state_selected="true"/>
    <item android:drawable="@color/primary_light_transparent" android:state_activated="true" android:state_enabled="true"/>
    <item android:drawable="@drawable/normal"/>
</selector>

布局XML是:

<FrameLayout
android:id="@+id/keypad_6"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:background="@drawable/button_selector"
     android:descendantFocusability="beforeDescendants"
     android:onClick="onClick" >

     <Button
     android:id="@+id/keypad_6_bt"
     style="@style/Button.Keypad.Numeric"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:onClick="onClick"
     android:text="6" />

    <TextView
     android:id="@+id/keypad_6_tv"
     style="@style/Keypad_Letters"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="bottom"
     android:gravity="center_horizontal"
     android:onClick="onClick"
     android:text="MNO"
     android:textColor="@color/keypad_digits_color" />
</FrameLayout>

我正在寻找的是,当 FrameLayout 的子视图(Button 或 TextView)被单击时,FrameLayout 的选择器应该工作并且应该突出显示整个布局。

我无法做到这一点,期待知道我该怎么做

添加 android:addStatesFromChildren="true" 到您的 FrameLayout

当按下 child 时,它获得焦点而不是 parent。 所以忘记使用选择器来做这件事

我对你的建议是: 你必须在你的 activityfragment 中以编程方式进行,方法是参考 parent 布局并根据您从 child 收听的状态以编程方式更改其背景。例如

FrameLayout parentLayout = findViewById(R.id.keypad_6);
Button button6 = (Button) findViewById(R.id.keypad_6_bt);
button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
         if (MotionEvent.ACTION_DOWN == event.getAction()){
                parentLayout.setBackgroundColor(Color.BLACK);
            }else if (MotionEvent.ACTION_UP == event.getAction()){
                parentLayout.setBackgroundColor(Color.white);
            }
            return false;
        }
    });