1

我创建与这张图片相同的 Ui:

在此处输入图像描述

下面是我的 xml 源代码,只是告诉我如何设置点击侦听器?所以用户点击图片列上的任意位置或图片列或视频列上的任意位置开始另一个活动?

             <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@drawable/main_background"
 android:orientation="vertical" >

 <include
    android:id="@id/includeTop"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    layout="@layout/private_space_title" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@id/mail_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/main_list_bg"
        android:orientation="vertical"
        android:paddingBottom="10.0dip"
        android:paddingLeft="20.0dip"
        android:paddingRight="20.0dip"
        android:paddingTop="15.0dip" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@id/picture_part"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/background_selector"
                android:clickable="true"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingRight="10.0dip" >

                <ImageView
                    android:id="@id/left_icon1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10.0dip"
                    android:src="@drawable/icon_picture" />

                <TextView
                    android:id="@id/textView1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="@string/function_img_management"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="@color/white" />

                <ImageView
                    android:id="@id/right_icon1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/listview_arrow" />
            </LinearLayout>

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/list_view_divider" />

            <LinearLayout
                android:id="@id/video_part"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/background_selector"
                android:clickable="true"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingRight="10.0dip" >

                <ImageView
                    android:id="@id/left_icon2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10.0dip"
                    android:src="@drawable/icon_video" />

                <TextView
                    android:id="@id/textView2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="@string/function_video_management"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="@color/white" />

                <ImageView
                    android:id="@id/right_icon2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/listview_arrow" />
            </LinearLayout>

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/list_view_divider" />

            <LinearLayout
                android:id="@id/sms_call_part"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/background_selector"
                android:clickable="true"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingRight="10.0dip" >

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@id/left_icon2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="10.0dip"
                        android:src="@drawable/icon_sms_call" />

                    <TextView
                        android:id="@id/unread_count"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignRight="@id/left_icon2"
                        android:layout_marginRight="5.0dip"
                        android:layout_marginTop="3.0dip"
                        android:background="@drawable/counter"
                        android:gravity="center"
                        android:textColor="@color/white"
                        android:visibility="visible" />
                </RelativeLayout>

                <TextView
                    android:id="@id/textView3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="@string/function_sms_management"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="@color/white" />

                <ImageView
                    android:id="@id/right_icon3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/listview_arrow" />
            </LinearLayout>

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/list_view_divider" />

            <LinearLayout
                android:id="@id/private_contact_part"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/background_selector"
                android:clickable="true"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingRight="10.0dip" >

                <ImageView
                    android:id="@id/left_icon4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10.0dip"
                    android:src="@drawable/icon_contact" />

                <TextView
                    android:id="@id/textView4"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="@string/function_privacy_management"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="@color/white" />

                <ImageView
                    android:id="@id/right_icon4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/listview_arrow" />
            </LinearLayout>

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/list_view_divider" />

            <LinearLayout
                android:id="@id/break_in_part"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/background_selector"
                android:clickable="true"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingRight="10.0dip" >

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@id/left_icon5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="10.0dip"
                        android:src="@drawable/icon_break_in" />

                    <TextView
                        android:id="@id/login_record_main_unread_count"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignRight="@id/left_icon5"
                        android:layout_marginRight="5.0dip"
                        android:layout_marginTop="3.0dip"
                        android:background="@drawable/counter"
                        android:gravity="center"
                        android:textColor="@color/white"
                        android:visibility="visible" />
                </RelativeLayout>

                <TextView
                    android:id="@id/textView5"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="@string/function_break_in_management"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="@color/white" />

                <ImageView
                    android:id="@id/right_icon5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/listview_arrow" />
            </LinearLayout>

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/list_view_divider" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="90.0dip"
            android:layout_weight="1.0"
            android:gravity="center" >

            <Button
                android:id="@id/member_up_btn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20.0dip"
                android:layout_marginRight="20.0dip"
                android:background="@drawable/upgrade_selector"
                android:text="@string/main_upgrade_button"
                android:textSize="18.0sp" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

 </LinearLayout>
4

3 回答 3

2

只需为每个 linearLayout 容器绑定一个点击事件:picture_part、video_part、sms_call_part...

例如 :

    LinearLayout menu_photos = (LinearLayout )findViewById(R.id.picture_part);
    menu_photos.setOnClickListener(new OnClickListener() {      
        @Override
        public void onClick(View v) {
            Intent picture_intent = new Intent(CurrentActivity.this,PictureActivity.class);
startActivity(picture_intent );     
        }
    });

对其他项目使用相同的方法。

于 2013-03-23T20:13:59.583 回答
1

您可以onClick使用相应的方法为要单击的每个视图设置属性。例如:

android:onClick="viewOnClickMethod"

并在你的Activity类中定义方法:

public void viewOnClickMethod (View v)
{
    //from here start any activity you want
}

对于并非设计为可点击的视图,您必须设置:

android:clickable="true"

财产也是如此。

于 2013-03-23T19:39:58.157 回答
-1

您应该使用 listView 和OnItemSelectedListener

通过这种方式,您可以避免编写如此多的 XML,也可以避免为每一行设置一个侦听器。

在侦听器中,使用startActivity ()检查选择了哪个项目并启动您希望的活动。如果活动属于您的应用,请将其信息放入清单文件中。

此外,您可以使用操作栏而不是自定义的上栏。

于 2013-03-23T19:43:44.177 回答