android - 在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>
解决方案
对于要在 Java 代码中使用的每个视图,您都应该有一个 id。然后您可以在活动中使用 findViewById 方法找到该视图。因此,对于您的视图,您应该为第二个 textView 考虑一个 id,例如 tvDisplay,然后在您的 onClickListener 中,您应该通过如下代码将文本设置到您的 textView 中:
((TextView)findViewById(R.id.tvDisplay)).setText("hello")
推荐阅读
- flutter - 当我单击按钮并使其可见时,颤振变换定位小部件从左到右?
- javascript - 使用密码或身份验证服务保护网页
- unit-testing - 如何测试使用自定义 dataProvider 和多个资源的组件?
- mysql - MYSQL:如何将包含 HTML 的列导出到 CSV 或 Excel?
- guidewire - Guidewire PCF 上的确认消息
- python - Cython promlem:cimport libcpp.vector 未编译
- java - 使用带有 keyGenerator 和除非属性的 spring @Cacheable
- neural-network - 如何在pytorch中单独批量旋转图像
- apache-kafka - 使用 Kafka 安全管理器进行 ACL 进行架构注册表
- hadoop - ERROR streaming.StreamJob:错误启动作业:不是文件hadoop