android如何使relativelayout不可点击,其itme可点击

时间:2013-01-24 18:08:11

标签: android android-layout

我有一个带有relativelayout标题的列表视图,当我点击relativelayout中的任何空格它变成白色时,我不想要那个白色,如果用户点击空格,什么都不会发生,但如果他点击了将图像转换为相对布局,我可以在onclick上处理它。

这是相对布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rl_simple_list_item_header"
    android:focusable="true"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/orderMeal"
        android:layout_marginLeft="20dip"
        android:id="@+id/tv_restaurant_description_orderMeal"
        android:drawableTop="@drawable/order_meal" />
    <TextView 
        android:id="@+id/iv_simple_list_item_header_favorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/favorite"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dip"
        android:drawableTop="@drawable/favorite"/>
</RelativeLayout>

修改

01-25 21:00:42.718: E/AndroidRuntime(24121): FATAL EXCEPTION: main
01-25 21:00:42.718: E/AndroidRuntime(24121): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Syriatel.EatTel/com.Syriatel.EatTel.Restaurant_Description}: java.lang.NullPointerException
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.os.Looper.loop(Looper.java:130)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.main(ActivityThread.java:3701)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at java.lang.reflect.Method.invokeNative(Native Method)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at java.lang.reflect.Method.invoke(Method.java:507)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at dalvik.system.NativeStart.main(Native Method)
01-25 21:00:42.718: E/AndroidRuntime(24121): Caused by: java.lang.NullPointerException
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.Syriatel.EatTel.Restaurant_Description.initialize(Restaurant_Description.java:46)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.Syriatel.EatTel.Restaurant_Description.onCreate(Restaurant_Description.java:35)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
01-25 21:00:42.718: E/AndroidRuntime(24121):    ... 11 more

3 个答案:

答案 0 :(得分:1)

尝试让RelativeLayout专注:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rl_simple_list_item_header"
    android:orientation="horizontal"
    android:focusable="true"
    android:focusableInTouchMode="true" >

<强>更新

如果您想在标题中点击TextView,可以这样做:

View header = LayoutInflater.from(this).inflate(R.layout.simple_list_item_header, null);
View favorite = header.findViewById(R.id.iv_simple_list_item_header_favorite);

lv_restaurantInformation = (ListView) findViewById(R.id.lv_restaurant_description_information);
lv_restaurantInformation.addHeaderView(header);

favorite.setOnClickListener(this);

然后在onClick

Activity方法中处理它
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.iv_simple_list_item_header_favorite:
        Toast.makeText(this, "onClick", Toast.LENGTH_SHORT).show();
        break;

    default:
        break;
    }
}

答案 1 :(得分:1)

如果你将onclicklistener分配给实际的项目,而不是在活动中实现它,这应该不是问题。

tv_favorite = (TextView) header.findViewById(R.id.iv_simple_list_item_header_favorite);
tv_favorite.setOnClickListener( new OnClickListener(){
    @Override
public void onClick(View v) {
        tv_favorite.setText("asdfasdfasdf");
}

});

如果你想要ListView中的单个项目(而不是标题),你应该使用为整个ListView设置的OnItemClickListener。

答案 2 :(得分:1)

设置布局可点击,没有任何onClickListener

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/info1"
            android:clickable="true">