【问题标题】:How to set a xml layout as the view to a linear layout如何将 xml 布局设置为线性布局的视图
【发布时间】:2018-05-29 20:41:11
【问题描述】:

我正在做一个项目,我想在一个页面中创建两个部分,例如,我将使用给定的部分创建一个主页(下面的 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/>

【问题讨论】:

  • 不明白你想用你的底部布局做什么
  • 首先:不确定您的示例中的布局权重是否正常工作...考虑使用带有权重和的 LinearLayout,使用垂直的 android:orientation... 然后,在下面制作您的线性布局,高度为 0dp,以便重量按您的意愿工作 :)...其次:查看 控件,它使您能够包含您制作的布局,而不是您正在使用的 LinearLayout现在。干杯!

标签: android xml android-layout android-linearlayout


【解决方案1】:

正如我从帖子中了解您的问题,我建议您为底部视图创建单独的布局并将其包含在您的主 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 文件中包含布局的所有控件

【讨论】:

  • 我该怎么做,请告诉我。
猜你喜欢
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-28
  • 1970-01-01
相关资源
最近更新 更多