Android layout as button with selectable functionality

有些话、适合烂在心里 提交于 2019-12-03 10:45:38

You can add android:duplicateParentState="true" to the ImageView widget. Also you need to make the ImageView's parent clickable and focusable.

The RelativeLayout in the following code will act as a Button:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout:height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true">

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:duplicateParentState="true"
            android:src="@drawable/icon" />
        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/image"
            android:layout_alignTop="@+id/image"
            android:layout_alignWithParentIfMissing="true"
            android:duplicateParentState="true" />
        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"            
            android:layout_toRightOf="@+id/image"
            android:layout_below="@+id/text1"
            android:layout_alignWithParentIfMissing="true"
            android:duplicateParentState="true" />
    </RelativeLayout>
</LinearLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!