安卓布局常用代码介绍7——相对布局的使用例子

版权声明:私聊~~ https://blog.csdn.net/weixin_42875245/article/details/81534132

效果如下:

这里写图片描述

前提:

1.因为后台需要提取id,每个控件都需要使用,如果使用相对布局,会不易于后期调整布局,所以主要使用线性布局;
2.因为线性布局虽然很工整,但是有时候不容易居中等操作,所以使用相对布局。

设计思路:

1.首先使用线性布局垂直分布
2.然后使用三个线性布局用weight按照3:4:3均匀分布;
3.对于中间那个线性布局同样使用垂直分布
4.在中间那个线性布局插入四个线性布局,一个相对布局。

代码如下:

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3">

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="用户登录"
                android:layout_marginBottom="10dp"
                android:textColor="#000000"
                android:textSize="24sp" />

            <EditText
                android:id="@+id/account"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="用户名"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:maxLines="1"
                />

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:hint="密码"
                android:maxLines="1"/>

            <Button
                android:id="@+id/login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="登录"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/text_View_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="丨"
                    android:textColor="#000000"
                    android:textSize="30sp"
                    />


                <Button
                    android:id="@+id/button_3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/text_View_2"
                    android:background="#00000000"
                    android:text="忘记密码"
                    android:textColor="#000000"
                    />

                <Button
                    android:id="@+id/register"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toLeftOf="@id/text_View_2"
                    android:background="#00000000"
                    android:text="我要注册"
                    android:textColor="#000000"
                    />

            </RelativeLayout>


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3">

        </LinearLayout>

        <TextView
            android:id="@+id/textView_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="登录即代表阅读并同意服务条款"
            android:textColor="#cfcfcf"
            android:textSize="13sp"
            />

    </LinearLayout>

猜你喜欢

转载自blog.csdn.net/weixin_42875245/article/details/81534132
今日推荐