我開發了一個可以具有圖片和文本的按鈕的自定義控件,因爲我沒有找到可以做到這一點的標準控件。它很好用,但我現在遇到的情況是,單擊按鈕時,textview不會將其狀態更改爲按下狀態。我想要有這種行爲,以便我可以爲textview應用ColorStateList,以便按下按鈕時文本顏色會發生變化。 這是自定義按鈕的我的XML佈局:Android:如何將狀態從按鈕鏈接到相同佈局的textview狀態
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_text_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="@drawable/btn_background"
android:visibility="visible"
>
<LinearLayout android:id="@+id/layoutContent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="3dp"
android:duplicateParentState="true">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:duplicateParentState="true"
android:gravity="bottom|center_horizontal"
android:src="@drawable/testing_teaser1"
/>
<TextView
android:id="@+id/textField"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:duplicateParentState="true"
android:gravity="center"
android:text="This is a ImageTextButton"
android:textColor="@color/black" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/btn_bg_selector"
/>
</FrameLayout>
我怎樣才能TextView的鏈接到按鈕的狀態? android:duplicateParentState="true"
不起作用,因爲該按鈕不是textview的父級。
乾杯花花公子
非常好的解決方案。奇蹟般有效!但必須將Button設置爲'android:clickable = false',以便它不攔截點擊。 – Dude