2012-07-19 33 views
2

我想知道是否有方法同時選擇(突出顯示單擊)ImageButton和TextView。我有一個ImageButton和TextView。我把它們放在一個RelativeLayout中,並將背景設置爲一個可繪製的選擇器xml。但他們沒有一起強調。如果我按下ImageButton按鈕,它會單獨突出顯示,如果按下佈局,則突出顯示而不突出顯示ImageButton。你能在這方面幫助我嗎?同時選擇ImageButton和TextView

謝謝。

+0

你可以發佈你的動作監聽器代碼 – Vinay 2012-07-19 08:10:41

+0

確保你的'RelativeLayout'是可點擊的,並將onClickListener分配給你的佈局而不是Text或ImageView。 – DroidBender 2012-07-19 08:13:44

回答

2

我有解決方案爲您

  • 包裹ImageButton和 'TextView的' 在 '的LinearLayout'
  • 禁用clickable ,的ImageButton的focusable事件和TextView的
  • LinearLayout

應用自定義選擇在這裏不用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/toast_layout_root" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#f0f0f0" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:padding="10dp" > 

    <LinearLayout 
     android:id="@+id/myCustomComponent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:background="@drawable/layout_bgimage_selector" 
     android:clickable="true" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:gravity="center" 
     android:orientation="vertical" > 

     <ImageButton 
      android:id="@+id/testImage" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:src="@drawable/icon" /> 

     <TextView 
      android:id="@+id/testText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:text="Test Text" 
      android:textColor="#FFF" /> 
    </LinearLayout> 

</RelativeLayout> 

這個佈局文件使用自定義選擇XML文件layout_bgimage_selector.xml,這需要更新custom_component.xml佈局文件置於res/drawable文件夾中

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/background" android:state_enabled="false"/> 
    <item android:drawable="@drawable/background" android:state_focused="true" android:state_pressed="true"/> 
    <item android:drawable="@drawable/background_over" android:state_enabled="true" android:state_pressed="true"/> 
    <item android:drawable="@drawable/background_over" android:state_enabled="true" android:state_focused="true"/> 
    <item android:drawable="@drawable/background" android:state_enabled="true"/> 

</selector> 

我用於佈局選擇器9補丁圖像是:

background.9.png - background.9.png

background_over.9.png - background_over.9.png

代替它們在res/drawable-hdpi文件夾中。 (或任何圖像資源文件夾匹配您的設備配置)

+0

非常感謝。有效! – 2012-07-19 11:47:43

1

使用了TextView的父佈局和ImageView的

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
> 
    <item 
     android:state_focused="true" 
     android:state_pressed="false" 
android:drawable="@drawable/listview_background_focused"  /> 
    <item 
     android:state_focused="true" 
     android:state_pressed="true" 
android:drawable="@drawable/listview_background_focused"  /> 
    <item 
     android:state_focused="false" 
     android:state_pressed="true" 
android:drawable="@drawable/listview_background_focused"  /> 
    <item 
android:drawable="@drawable/listview_background_unfocused"  /> 
</selector> 

譜寫父佈局,而不是子視圖的onclickListener各自的代碼的自定義背景。

0

我建議聲明一個包含文本和圖像的按鈕。

<Button 
     android:id="@+id/dash_bt_list" 
     android:drawableTop="@drawable/your_image" 
     android:background="@drawable/selector" 
     android:onClick="onClickFeature" 
     android:text="@string/your_text" 
     android:layout_width="wrap_content" /> 

而在你的選擇:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_focused="true" android:state_pressed="true"> 
      <shape> 
       <solid android:color="#FFFFFF" /> 
       <stroke android:width="3dp" android:color="#000000" /> 
       <padding android:bottom="1dp" android:left="1dp" android:right="1dp" 
        android:top="1dp" /> 
      </shape> 
      ... 
    </selector> 

,幫助我

相關問題