天天看点

安卓布局常用代码介绍6——线性布局使用代码例子

效果如下:

安卓布局常用代码介绍6——线性布局使用代码例子

设置思路:

先利用线性布局先垂直排列

第一行插入一个线性布局:水平排列,用比例排列;

第二行插入一个线性布局:垂直排列,用weight=1垂直;

中间这个线性布局就各种微调。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="输入日期:"
            android:textSize="18sp"/>

        <EditText
            android:id = "@+id/getyear"
            android:textColor = "#999999"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:hint="年份"

        <EditText
            android:id = "@+id/getmonth"
            android:textColor = "#999999"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="月份"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"

        <EditText
            android:id = "@+id/getdate"
            android:textColor = "#999999"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="日期"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"

    </LinearLayout>

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

        <Button
            android:id="@+id/btn_set"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="设置"

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="50dp"
            android:text="距考研仅剩"
            android:textSize="24sp"
            android:textColor="#000000"

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/time_output"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="80dp"
                android:text="100"
                android:textSize="100sp"
                android:background="#0000"
                android:layout_marginTop="0dp"

            <TextView
                android:id="@+id/text_View_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="0dp"
                android:text="天"
                android:textSize="24sp"
                android:textColor="#000000"

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/days_target"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#0000"
                android:layout_marginLeft="100sp"
                android:text="目标日:"
                android:textSize="20sp"

            <TextView
                android:id="@+id/days_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:background="#0000"
                android:gravity="center"
                android:text="2018-12-22"
                android:textSize="20sp"

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/tip"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="贴士"
            android:textSize="20sp"
            android:background="#00000000"

        <Button
            android:id="@+id/main"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="首页"
            android:textSize="20sp"

        <Button
            android:id="@+id/note"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="随笔"
            android:textSize="20sp"
            android:background="#00000000"

    </LinearLayout>


</LinearLayout>