首页 > 解决方案 > 在android中实现两个线性布局之间的关系

问题描述

我的应用程序中有两个 LinearLayouts 并排。在第一个线性布局(左侧)中,我有一些名称列表。我想以这样的方式实现它们之间的连接或关系,当我单击其中一个名称列表项时,一条文本消息或一些设计将显示到第二线性布局(右侧)。我已经用 Toast 实现了 OnClickListener,但我不知道在第二个线性布局中显示文本消息或某些设计。提前感谢您的帮助!

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/relLayoutMiddle"  android:orientation="horizontal"
            android:layout_below="@+id/relLayoutTopBar"
            android:layout_above="@+id/relLayoutBottomBar">

            <!-- Items List-->
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="80dp"
                android:layout_height="fill_parent"
                android:layout_marginRight="5dp"
                android:background="@drawable/border_design">    
                <ListView
                    android:id="@+id/item_List" android:scrollbars="none"
                    android:layout_width="match_parent"
                    android:focusable="true"
                    android:layout_height="fill_parent">
                </ListView>
            </LinearLayout>

            <!-- Display Items-->
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" android:layout_marginLeft="5dp"
                android:background="@drawable/border_design">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Items  will be Display here"/>
            </LinearLayout>
        </LinearLayout>

标签: androidandroid-layoutandroid-linearlayout

解决方案


对于要在 Java 代码中使用的每个视图,您都应该有一个 id。然后您可以在活动中使用 findViewById 方法找到该视图。因此,对于您的视图,您应该为第二个 textView 考虑一个 id,例如 tvDisplay,然后在您的 onClickListener 中,您应该通过如下代码将文本设置到您的 textView 中:

((TextView)findViewById(R.id.tvDisplay)).setText("hello")

推荐阅读