Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.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 布局类型的帮助_Java_Android_Layout - Fatal编程技术网

Java 布局类型的帮助

Java 布局类型的帮助,java,android,layout,Java,Android,Layout,对于这样的布局,我应该使用什么类型的布局? 我应该使用线性布局还是相对布局?你能解释一下你为什么选择这种布局吗。一个样本也会有帮助 谢谢如果你的布局像上面显示的那样简单,我会使用LinearLayout-主要是因为我发现使用相对布局有点烦人,更难在其中找到bug 一个样本是: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" and

对于这样的布局,我应该使用什么类型的布局?

我应该使用线性布局还是相对布局?你能解释一下你为什么选择这种布局吗。一个样本也会有帮助


谢谢

如果你的布局像上面显示的那样简单,我会使用LinearLayout-主要是因为我发现使用相对布局有点烦人,更难在其中找到bug

一个样本是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:id="@+id/linearLayout1"
    android:orientation="vertical" android:layout_weight="1"
    android:background="@android:color/darker_gray">
         <!-- Put widgets here -->
    </LinearLayout>
    <LinearLayout android:layout_height="100dp"
    android:layout_width="fill_parent" android:id="@+id/linearLayout2"
    android:orientation="vertical" android:background="@android:color/background_light">
         <!-- Put widgets here -->
    </LinearLayout>
</LinearLayout>



两个注意事项,第一个线性表示高度为包裹内容,这不意味着它会随着内容的添加而扩展吗?第二条直线是否位于屏幕底部?它看起来只是位于顶部的下方。这里的诀窍是第一个线性布局的布局。这是为了告诉布局占用所有剩余的可用空间,而不管其内容或自身的布局高度值如何。这也会将第二个布局定位在屏幕底部,因为它只会占用100dp的高度,而将其余部分留给第一个布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
    <RelativeLayout android:layout_width="match_parent"
        android:background="#ff00" android:id="@+id/relativeLayout1"
        android:layout_height="100dp" android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"></RelativeLayout>
    <RelativeLayout android:layout_width="match_parent"
        android:background="#f0f0" android:id="@+id/relativeLayout2"
        android:layout_height="wrap_content" android:layout_alignParentTop="true"
        android:layout_above="@+id/relativeLayout1"
        android:layout_centerHorizontal="true"></RelativeLayout>
</RelativeLayout>