Android布局为具有可选功能的按钮 [英] Android layout as button with selectable functionality
问题描述
我正在尝试创建一个类似按钮的组件,其左对齐的ImageView,然后在ImageView的右侧,两个TextView,一个堆叠在另一个之上,并采用不同的格式,例如以下示例:.
I'm trying to create a button-like component, with a left-aligned ImageView and then 2 TextViews to the right of the ImageView, stacked one above the other and formatted differently, like the following example:.
__________________________
| |
| |-----| Bold Main Text |
| |Image| |
| |-----| Small Sub Text |
|__________________________|
我还希望ImageView根据外部容器的单击状态进行更改,就像标准按钮将使用与之关联的可选资源一样.这样,当我单击外部框中的任意位置时,图像的可选状态就会更改.
I also want the ImageView to change depending on the click state of the outer container, much like a standard button would do with a selectable resource associated with it. So that when I click anywhere in the outer box the image selectable state is changed.
我知道我可以使用一个按钮,将'drawableLeft'属性设置为与按钮关联的与图像相关的一行文本,但是使用这种策略似乎只能包含一行文本.
I know I can use a Button, setting the 'drawableLeft' property to create a single line of text associated with an Image as a button, but it seems I can only have a single item of text using this strategy.
过去有没有人实现过这样的UI组件?
Has anyone implemented any UI components like this in the past?
谢谢!
推荐答案
您可以将android:duplicateParentState="true"
添加到ImageView
小部件中.另外,您还需要使ImageView
的父级成为可点击和可聚焦的.
You can add android:duplicateParentState="true"
to the ImageView
widget. Also you need to make the ImageView
's parent clickable and focusable.
以下代码中的RelativeLayout
将充当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>
这篇关于Android布局为具有可选功能的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!