2012-09-10 70 views
1

我使用圖庫來顯示聯繫人的信息,在項目視圖,有兩個按鈕,我用選擇器作爲他們的背景,現在這裏是問題,我觸摸區域出這兩個按鈕,這兩個按鈕會改變背景看起來像他們都被按下。 這裏是庫項目android選擇器

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#505050" 
    > 
    <ImageView 
    android:id="@+id/phone_contact_image" 
    android:layout_height="160dip" 
    android:layout_width="160dip" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="25dip" 
    android:src="@drawable/contact_phone_image_1" 
    /> 
    <TextView 
    android:id="@+id/phone_contact_name" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    android:textSize="25dip" 
    android:layout_below="@id/phone_contact_image" 
    android:layout_marginTop="10dip" 
    android:gravity="center_horizontal" 
    android:textColor="#FFFFFF" 
    /> 
    <TextView 
    android:id="@+id/phone_contact_numbler" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="25dip" 
    android:gravity="center_horizontal" 
    android:layout_below="@id/phone_contact_name" 
    android:text="@string/contact_phone_numbler" 
    android:textColor="#FFFFFF" 
    /> 
    <Button 
    android:id="@+id/phone_contact_edit_btn" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:text="@string/editContact_title_edit" 
    android:textSize="28dip" 
    android:background="@drawable/btn_button_bg" 
    android:layout_marginLeft="30dip" 
    android:layout_marginRight="30dip" 
    android:layout_marginTop="10dip" 
    android:textColor="#FFFFFF" 
    android:layout_below="@id/phone_contact_numbler" 
    /> 
    <Button 
    android:id="@+id/phone_contact_call_btn" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:text="@string/title_call_via" 
    android:textSize="28dip" 
    android:background="@drawable/btn_confirm_bg" 
    android:layout_marginTop="20dip" 
    android:layout_marginLeft="30dip" 
    android:layout_marginRight="30dip" 
    android:textColor="#FFFFFF" 
    android:layout_below="@id/phone_contact_edit_btn" 
    /> 
</RelativeLayout> 

這裏是選擇

<?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:drawable="@drawable/btn_button_focus" /> 
    <item android:state_pressed="true" android:drawable="@drawable/btn_button_pressed" /> 
    <item android:drawable="@drawable/btn_button_normal" /> 
</selector> 
+0

你嘗試以下建議的解決方案? –

+0

嗨,我發現了一些新的線索,我使用沒有background.but ..你可能看這個(https://www.dropbox.com/s/5cbwvchl8hrfz1e/2012-09-11%2009.52.53.mp4) – liaobz

+0

最終的解決方案是新的一個類從Button擴展,並覆蓋一個方法:@Override public void setPressed(boolean pressed){ //如果父按下,不要設置爲按下。 (按下&&((View)getParent())isPressed()){ return; } super.setPressed(pressed); } – liaobz

回答

0

你是對的。但是當你沒有按下狀態時,你沒有提及,沒有被選中,那麼你會做什麼?

試試這個: 做出styles.xml文件在你res.values文件夾 和添加。

<style name="styleNormalButton"> 
     <item name="android:background">@drawable/backgroundbutton</item> 
    </style> 

考慮@drawable/backgroundbutton.xml是你的選擇文件,並在添加下面的代碼。

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:drawable="@drawable/btn_button_focus" /> 
    <item android:state_pressed="true" android:drawable="@drawable/btn_button_pressed"/> 

    <item android:drawable="@drawable/btn_default_pressed_holo_dark" 
     android:state_enabled="false" /> 
    <item android:drawable="@drawable/btn_button_normal" /> 
</selector> 

最後進入你的layou文件。 設置按鈕來style/styleNormalButton

希望的背景下,這將幫助你

+0

我剛剛嘗試過,看起來不起作用。我認爲state_enabled是爲禁用做準備的,所以我忽略它...我錯過了一些eles嗎? – liaobz

+0

然後看看是不是還是按照自下而上的辦法。我的意思是設置狀態爲焦點並按下爲假,並在那裏給出默認背景。試試這個,讓我知道 –

+0

你是不是這個意思?不工作... \t \t \t liaobz