我正在尝试创建一个类似按钮的组件,其中有一个左对齐的 ImageView,然后是 ImageView 右侧的 2 个 TextView,一个堆叠在另一个上方,格式不同,如下例所示:
__________________________
| |
| |-----| Bold Main Text |
| |Image| |
| |-----| Small Sub Text |
|__________________________|
我还希望 ImageView 根据外部容器的点击状态而改变,就像标准按钮处理与之关联的可选资源一样。这样当我点击外框的任何地方时,图像可选状态就会改变。
我知道我可以使用 Button,设置 'drawableLeft' 属性来创建与图像关联的单行文本作为按钮,但似乎我只能使用此策略来处理单个文本项。
过去有没有人实现过类似的 UI 组件?
谢谢!
最佳答案
您可以将 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>
关于Android 布局作为具有可选功能的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8821954/