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

时间:2015-04-01 09:29:18

标签: android android-layout

选择器的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的选择器应该工作,并且应该突出显示整个布局。

我无法实现这一点,并期待知道我该怎么做

2 个答案:

答案 0 :(得分:3)

添加 android:addStatesFromChildren="true" FrameLayout

答案 1 :(得分:0)

当孩子被按下时,焦点不是父母。 所以忘记使用选择器

这样做

我的建议是:您必须在activityfragment内以编程方式执行此操作,方法是为父布局提供参考,并按照编程方式更改其背景到你从孩子那里听的状态。例如

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;
        }
    });