Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android中两个线性布局的相同onClick方法_Java_Android_Android Linearlayout - Fatal编程技术网

Java Android中两个线性布局的相同onClick方法

Java Android中两个线性布局的相同onClick方法,java,android,android-linearlayout,Java,Android,Android Linearlayout,我有两个这样的线性布局: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:background="@drawable/border" android:orientation="horizontal"

我有两个这样的线性布局:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="@drawable/border"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="2"
        android:onClick="viewTipsDetail">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:src="@mipmap/img_breakfast" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Đừng bỏ bữa sáng"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="false"
                android:text="Các nghiên cứu cho thấy bữa sáng là một những điê"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="@drawable/border"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="2"
        android:onClick="viewTipsDetail">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:src="@mipmap/img_waterglass" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Uống đủ nước"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity=""
                android:singleLine="false"
                android:text="Bổ sung đủ nước cho cơ thể, ngay cả các bạn làm việc trong môi trường máy lạnh hoặc ngoài trời. Việc mất nước có thể khiến bạn căng thẳng, khó tập trung vào công việc"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>
我打算用相同的方法为它们实现一个onClickEvent。在这种情况下,事件将调用ViewTipDetail方法。那么,我如何用一种方法获得图像src,即特定线性布局中的文本?有没有任何可能的方法可以自动生成这样一个具有给定细节的线性布局

这是一个示例,我将使用更多的LinearLayout,所以我通常需要一种可能的方法来获取特定LinearLayout中的细节


或者只是使用列表视图来实现…

为什么不使用onClickListener来实现这一点?使用不同的线性布局id并管理onClickListener这是一个示例,将有很多线性布局,如何为所有这些布局编写方法?您是从xml还是以编程方式添加更多的线性布局?是的,当然。将会有更多的线性布局,这就是为什么我不能使用id并为所有它们编写一个onClick事件。这是一个规则,没有给定的id,你不能访问@NguyenHoangVu-k11fughcmb,但你为什么这样做?多子布局-为了什么?给我一个例子来理解你的想法:不要使用Listview,你可以使用Recyclerview它对你来说是一个好东西@NguyenHoangVu-K11FUGHCMAnyway-如果你想实现你的想法,你应该把你的线性布局放在ScrollView中垂直滚动。这是可能的,但你会为这件简单的事情付出太多的努力。@NguyenHoangVu-K11FUGHCM请参阅此链接以了解batter recyclerview:
<LinearLayout
        id="@+id/layout1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.5"
        onClick="onLayoutClick"
        android:orientation="vertical">

<LinearLayout
        id="@+id/layout2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.5"
        onClick="onLayoutClick"
        android:orientation="vertical">
@Override
public void onLayoutClick(View view) {
    // click click
    // view.getTag()
}