1

画像とテキストを表示できるボタン用のカスタム コントロールを開発しました。それができる標準のウィジェットが見つからなかったからです。それはうまく機能しますが、私が今遭遇したのは、ボタンがクリックされたときにテキストビューが押された状態に変わらないということです。ボタンが押されたときにテキストの色が変わるように、テキストビューにColorStateListを適用できるように、この動作が必要です。これは、カスタム ボタンの私の xml レイアウトです。

<?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>

テキストビューをボタンの状態にリンクするにはどうすればよいですか? android:duplicateParentState="true"ボタンはテキストビューの親ではないため、機能しません。

乾杯男

4

3 に答える 3

4

をに設定するかOnClickListenerLinearLayoutまたはFrameLayoutに設定する代わりにButtonButtonxmlで追加する必要があります android:duplicateParentState="true"

于 2012-10-24T06:49:14.497 に答える
1

まさにそれを行うウィジェットがあることをご存知ですか? ( ImageButton)

それでもカスタム コンポーネントを使いたい場合は、"@drawable/btn_bg_selector" を親に割り当て、 .の代わりにFrameLayoutそのレイアウトを作成する必要があります。clickableButton

于 2012-10-24T06:42:01.183 に答える
-1

ボタンのxmlに属性を追加します

android:onClick="clickAction" 

アクティビティには次の機能が含まれます

public void clickAction(View view){
   //Code whatever you want when the button is clicked
}
于 2012-10-24T06:44:29.533 に答える