我正在尝试创建一个类似按钮的组件,在ImageView右侧有一个左对齐的ImageView,然后是2个TextView,一个堆叠在另一个上面,格式不同,如下例所示:。
__________________________
| |
| |-----| Bold Main Text |
| |Image| |
| |-----| Small Sub Text |
|__________________________|
我还希望ImageView根据外部容器的单击状态进行更改,就像标准按钮可以使用与之关联的可选资源一样。因此,当我单击外框中的任何位置时,图像可选状态会发生变化。
我知道我可以使用Button,设置'drawableLeft'属性来创建与Image相关联的单行文本作为按钮,但似乎我只能使用此策略的单个文本项。
过去有没有人实现过这样的UI组件?
谢谢!
答案 0 :(得分:14)
您可以将android:duplicateParentState="true"
添加到ImageView
窗口小部件。此外,您还需要使ImageView
的父级可点击并且可以关注。
以下代码中的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>