【问题标题】:several controls in one with drawable background (gradient)多个控件合二为一,具有可绘制背景(渐变)
【发布时间】:2011-06-06 18:59:17
【问题描述】:

您好,我正在尝试完成以下任务:

我有一个应该呈现 3 个 android 标准控件的控件:imageview 和一个 textview。我试图将它们包装在一个带有渐变背景的控件中,所以它看起来像一个按钮。

这是我尝试过的: 在控件的布局文件中,我添加了一个按钮

<Button android:id="@+id/btnCustomButton" android:layout_width="214dp" android:drawable="@drawable/custom_button" android:background="@drawable/custom_button_background" android:gravity="center_horizontal" android:layout_height="39dp" />

在 custom_button.xml 中:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:height="33dp" android:width="214dp">
   <ImageView
  android:gravity="left|center_horizontal" android:id="@+id/btn_icon" android:layout_width="fill_parent"  android:layout_height="27dp" />

   <TextView android:id="@+id/txtLabel" android:textSize="15px" android:textColor="@color/main_text_black" android:text="Search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:singleLine="true" />
</shape

在 custom_button_background.xml 中:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="15dp" android:state_pressed="true">
            <shape android:shape="rectangle">
              <gradient android:startColor="@color/gradient_hl_top" android:endColor="@color/gradient_hl_bottom" android:angle="270" />
            <stroke android:width="3dp" android:color="@color/main_text_black" />
            <corners android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
            </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle">
              <gradient android:startColor="@color/gradient_hl_top" android:endColor="@color/gradient_hl_bottom" android:angle="270" />
            <stroke android:width="3dp" android:color="@color/main_text_black" />
            <corners android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
            </shape>
    </item>
    <item>        
        <shape android:shape="rectangle">
              <gradient android:startColor="@color/_gradient_top" android:endColor="@color/gradient_bottom" android:angle="270" />
            <stroke android:width="3dp" android:color="@color/main_text_black" />
            <corners android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
            </shape>
    </item>
</selector>

我看不到 TextView 打印“搜索”,但我看到渐变工作。我做错了什么?

【问题讨论】:

    标签: android controls selector shape


    【解决方案1】:

    这就是我的做法:

    1. 我创建了一个具有相对布局的自定义控件,将布局的背景设置为选择器 custom_button_background.xml

    2. 在自定义控件的相关布局xml文件中添加了ImageView和TextView

    3. 在控件中添加了点击监听

    问题是我无法看到突出显示,因为未处理点击。

    我现在唯一的问题是 ImageView 有自己的高亮状态(不同的图像)并且也需要一个选择器。当我向它添加选择器时,我在单击时看不到 ImageView 上的任何变化。一旦我找出问题,我会添加其余的。

    【讨论】:

    • 所以没有应用选择器的原因是因为没有处理ImageView的点击。我真的不需要它。有没有办法在单击自定义控件时使所有控件单击(以模仿同步单击)或将控件添加到背景选择器?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    相关资源
    最近更新 更多