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打印「搜索」,但我看到漸變工作。我究竟做錯了什麼?
所以選擇器不應用的原因是因爲沒有處理ImageView的點擊。但我並不需要它。有沒有辦法讓單擊自定義控件時單擊所有控件(模仿同步點擊)或將控件添加到背景選擇器? – dropsOfJupiter 2011-01-16 07:37:00