简体   繁体   English

按钮的TextColor不会更改父级LinearLayout的onclick

[英]TextColor of button is not changing onclick of parent LinearLayout

I want to change the textcolor of button and modify image when I click on its parent linear layout. 当我单击其父级线性布局时,我想更改按钮的文本颜色并修改图像。 Image is changing but textcolor is not getting changed on click of linear layout. 单击线性布局时,图像正在更改,但textcolor并未更改。

Here is my code in xml - 这是我在xml中的代码 -

<LinearLayout
        android:id="@+id/btn_layout"
        android:layout_width="0.0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:gravity="center"
        android:clickable="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/done_with_accounts_action"
            android:textColor="@drawable/done_button_text_selector"
            android:focusable="false"
            android:textSize="@dimen/done_button_text_size" />

        <ImageView
            android:id="@+id/done_btn_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:paddingLeft="4dp"
            android:focusable="false"
            android:src="@drawable/done_btn_arrow_bg_selector" />
    </LinearLayout>

**selector xml for button -**

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@color/done_btn_pressed_color" />
    <item android:state_pressed="true" android:color="@color/done_btn_pressed_color" />
    <item android:color="@color/button_text_white_color" />
</selector>

If I replace button with TextView, everything works fine. 如果我用TextView替换按钮,一切正常。

In this lines: 在这行中:

 <item android:state_focused="true" android:color="@color/done_btn_pressed_color" />
    <item android:state_pressed="true" android:color="@color/done_btn_pressed_color" />

You put the same color done_btn_pressed_color . 您输入了相同的颜色done_btn_pressed_color Maybe thats the problem? 也许那是问题所在?

I found the solution - I have to set clickable as false for the done button.Now it also recognizes its parent onClick event. 我找到了解决方案-我必须将完成按钮设置为clickable,现在它也可以识别其父onClick事件。

Why the above code worked with textView because its onClick event is disabled byDefault, so it recognizes its parent onClick event. 为何上面的代码与textView一起使用,因为默认情况下禁用了它的onClick事件,因此它可以识别其父onClick事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM