14

我正在尝试创建一个类似按钮的组件,其中有一个左对齐的 ImageView,然后是 ImageView 右侧的 2 个 TextView,一个堆叠在另一个之上,并且格式不同,如下例所示:

 __________________________
|                          |
| |-----|  Bold Main Text  | 
| |Image|                  |
| |-----|  Small Sub Text  |
|__________________________|

我还希望 ImageView 根据外部容器的单击状态进行更改,就像标准按钮对与之关联的可选资源所做的那样。因此,当我单击外框中的任意位置时,图像可选状态会发生变化。

我知道我可以使用按钮,设置“drawableLeft”属性来创建与图像关联的单行文本作为按钮,但似乎使用此策略我只能有一个文本项。

过去有没有人实现过这样的 UI 组件?

谢谢!

4

1 回答 1

16

您可以添加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>
于 2012-01-11T17:07:27.873 回答