有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

关于布局类型的java帮助

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

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

谢谢


共 (2) 个答案

  1. # 1 楼答案

    <?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>
    
  2. # 2 楼答案

    如果您的布局像上面所示的那样简单,我会使用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>