![](/img/trans.png)
[英]Android: onClick on LinearLayout with TextView and Button
[英]TextColor of button is not changing onclick of parent LinearLayout
當我單擊其父級線性布局時,我想更改按鈕的文本顏色並修改圖像。 單擊線性布局時,圖像正在更改,但textcolor並未更改。
這是我在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>
如果我用TextView替換按鈕,一切正常。
在這行中:
<item android:state_focused="true" android:color="@color/done_btn_pressed_color" />
<item android:state_pressed="true" android:color="@color/done_btn_pressed_color" />
您輸入了相同的顏色done_btn_pressed_color
。 也許那是問題所在?
我找到了解決方案-我必須將完成按鈕設置為clickable,現在它也可以識別其父onClick事件。
為何上面的代碼與textView一起使用,因為默認情況下禁用了它的onClick事件,因此它可以識別其父onClick事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.