android - 如何将 xml 布局设置为线性布局的视图
问题描述
我正在开发一个项目,我想在一个页面中创建两个部分,例如,我将使用给定的部分(下面的 xml 代码)创建一个主页。它将在main_activity.xml
文件上完成,我想将底部的视图设置为LinearLayout
不同的布局资源文件。其中将包含一些不同的代码。我怎样才能做到这一点?
<RelativeLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
<LinearLayout>
android:id="@+id/Top"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
</LinearLayout>
<LinearLayout>
android:id="@+id/Bottom"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
</LinearLayout>
</RelativeLayout/>
解决方案
正如我从帖子中了解您的问题,我建议您为底部视图创建单独的布局并将其包含在您的主 xml 布局文件中
在这里,如果我想分别添加 2 个顶视图和底视图,您可以在布局中添加 2 个文件
假设我有两个布局文件 top_view.xml 和 bottom_view.xml
top_view.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="top button"/>
</LinearLayout>
bottom_view.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bottom button"/>
</LinearLayout>
并且在 main_activiy.xml 文件中包含这两个文件
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/d_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/top_view" />
<include
layout="@layout/bottom_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
您可以访问 main.java 文件中包含的布局的所有控件
推荐阅读
- python-3.x - 检查项目是否存在而不区分大小写
- ios - 谷歌地图显示位置,但不显示标记 - swift4
- linux-kernel - 内核试图执行受 NX 保护的页面——利用尝试?
- webpack - 为什么我的浏览器在使用 webpack 对节点模块进行代码拆分后显示空白页面?
- kubernetes - 一个 Kubernetes Ingress 面向多个集群?范围问题
- ios - 在 ionic 3 中使用 FilePicker-Phonegap-iOS-Plugin 进行离子服务时输入错误
- database - Cassandra 表中的最大分区数以及它如何依赖于磁盘空间?
- jenkins - Jenkins groovy 文件中的 File.exists() 不起作用
- forms - 如何在 Prestashop 1.6 上创建帐户时使用多个复选框
- python - 如何在 prometheus 中创建自定义指标?