2011-01-10 50 views
0

你好幾個控件我試圖完成以下任務:於一體的具有可繪製背景(梯度)

我有一個應該呈現3個Android的標準控件控制:ImageView的和一個TextView。我試圖用漸變背景將它們包裝在一個控件中,所以它看起來像一個按鈕。

這裏是我的嘗試: 在控件的佈局文件我在custom_button.xml增加了一個按鈕

<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" /> 

<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打印「搜索」,但我看到漸變工作。我究竟做錯了什麼?

回答

0

因此,這裏是我是如何做到這一點:

  1. 我創建了相對佈局自定義控件佈局設置背景選擇custom_button_background.xml

  2. 新增的ImageView和TextView的內自定義控件

  3. 添加於點擊收聽的相對佈局xml文件來控制

問題是我無法看到突出顯示,因爲點擊未處理。

我現在唯一的問題是ImageView有自己的高光狀態(不同的圖像),也需要一個選擇器。當我添加一個選擇器時,我無法在點擊時看到ImageView上的任何更改。一旦我找出問題,我會補充剩下的。

+0

所以選擇器不應用的原因是因爲沒有處理ImageView的點擊。但我並不需要它。有沒有辦法讓單擊自定義控件時單擊所有控件(模仿同步點擊)或將控件添加到背景選擇器? – dropsOfJupiter 2011-01-16 07:37:00