在一个有背景的绘制几个控件(梯度) [英] several controls in one with drawable background (gradient)

查看:158
本文介绍了在一个有背景的绘制几个控件(梯度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完成以下内容:

我有一个应该呈现3 Android的标准控件控制:ImageView的和一个TextView。我试图总结起来与渐变背景的一个控制,所以它看起来像一个按钮。

I have a control that supposed to render 3 android standard controls: imageview and a textview. I am trying to wrap them up in one control with gradient background , so it looks like one button.

下面是我的尝试:
在控件的布局文件我加了一个按钮

Here is what I tried: in the layout file for the control I added a button

<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:

in the 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:

in the 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印刷搜索,但我看到了梯度工作。我在做什么错了?

I can't see the TextView printing "Search" but I see the gradient working. What am I doing wrong?

推荐答案

因此​​,这里是我是如何做到这一点:

So here is how I did this:


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

  1. I created a custom control with relative layout, set background of the layout to a selector custom_button_background.xml

新增的ImageView和TextView的自定义控件的相对布局xml文件里面

Added ImageView and TextView inside the relative layout xml file of the custom control

添加上点击监听器来控制

Added on click listener to the control

问题是,我没能看到亮点,因为点击不处理的。

The problem was that I was not able to see the highlight because the click was not handled.

我现在唯一的问题是ImageView的都有自己的亮点状态(不同的图像),太需要一个选择。当我一个选择添加到它,我实在看不出上点击任何ImageView的变化。一旦我弄清楚这个问题,我会添加其余部分。

The only problem I have right now is the ImageView has its own highlight state (different image) and needs a selector too. When I add a selector to it I cannot really see anything changes on the ImageView on click. Once I figure out the problem I will add the rest.

这篇关于在一个有背景的绘制几个控件(梯度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆