2015-05-01 43 views
0

我有以下简单的选择:的Android选择自定义视图中不工作

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

和以下活动的XML:

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#FF0000" 
    android:weightSum="2"> 


    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:text="settings" 
     android:id="@+id/btnMainMenuSettings" 
     android:layout_gravity="center_horizontal" 
     android:background="@drawable/main_menu_button_selector" 
     android:layout_weight="1" /> 

    <com.test.myapp.custom_views.CustomMainMenuButton 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:text="settings" 
     android:layout_gravity="center_horizontal" 
     android:background="@drawable/main_menu_button_selector" 
     android:layout_weight="1" /> 
</LinearLayout> 

这是我的自定义视图:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_gravity="center" 
    android:gravity="center"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     style="@style/main_menu_button" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="@string/app_name" /> 
</LinearLayout> 

这里是java代码:

public class CustomMainMenuButton extends LinearLayout implements View.OnClickListener { 

    private Context mContext; 

    public CustomMainMenuButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mContext = context; 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.custom_main_menu_button, this, true); 
    } 

    @Override 
    public void onClick(View v) { 

    } 
} 

选择器对按钮正常工作,但对于自定义视图,我只获得正常状态,自定义视图不可点击,并且在点按自定义视图时没有任何反应。任何想法我怎么能使它工作?

回答

3

您的布局是无法点击,这是默认状态为所有布局。使用 setClickable(true)自定义视图实现或XML布局android:clickable="true"

你应该得到的东西,如:

<com.test.myapp.custom_views.CustomMainMenuButton 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:clickable="true" 
    android:text="settings" 
    android:layout_gravity="center_horizontal" 
    android:background="@drawable/main_menu_button_selector" 
    android:layout_weight="1" /> 
+0

谢谢大家。这一个伎俩。 – dsb

2

尝试改变选择为:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/main_menu_button_background_pressed" android:state_pressed="true"></item> 
    <item android:drawable="@drawable/main_menu_button_background_pressed" android:state_focused="true"></item> 
    <item android:drawable="@drawable/main_menu_button_background" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item> 
    <item android:drawable="@drawable/main_menu_button_background_pressed" android:state_enabled="false"></item> 
</selector> 
+0

谢谢添加android:clickable="true"!我的选择器突然停止工作(可能在我改变了一些东西但不知道是什么)之后,我尝试了所有可以在这里找到的解决方案,并且最终做到了。 再次感谢 –

1

<com.test.myapp.custom_views.CustomMainMenuButton 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:text="settings" 
     android:layout_gravity="center_horizontal" 
     android:background="@drawable/main_menu_button_selector" 
     android:layout_weight="1" /> 
相关问题