如何在半尺寸屏幕高度布局上设置布局

时间:2018-03-25 13:30:24

标签: android xml android-linearlayout

XML布局就像这样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/blue_bg">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Sign In"
            android:textAlignment="center"
            android:textColor="@color/white"
            android:textSize="28dp"
            android:layout_gravity="center"
        />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/white">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Login Card"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="28dp" />
    </LinearLayout>
</LinearLayout>

事实证明,第二个线性布局将放置在第一个布局的下方。我希望第一个布局尺寸为屏幕的一半,第二个布局将放置在第一个布局的底部而不是下面的第一个布局。

我正在尝试这样的Ui

enter image description here

如果有人知道如何解决这个问题。请在下面评论:)

3 个答案:

答案 0 :(得分:3)

<?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">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_light">

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/round_rect"
        android:layout_margin="20dp"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="name"
                android:textSize="20sp"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="password"
                android:textSize="20sp"/>
        </LinearLayout>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center"
            android:text="Sign in "
            android:background="@android:color/holo_blue_light"/>


    </LinearLayout>

</RelativeLayout>

You can see here output image that above code

答案 1 :(得分:1)

对这些类型的布局使用约束布局。从调色板拖放并在预览中修复。检查样本

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="0dp"
    android:layout_height="256dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:padding="20dp"
        android:text="SignIn"
        android:textColor="@android:color/white" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="293dp"
    android:layout_height="238dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="220dp"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:paddingTop="50dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.506"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.066">

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sign In" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

您可以使用Frame布局。内部使用颜色的线性布局,只有2个背景视图。然后在线性布局下方添加所有ui组件。

相关问题