Android LinearLayout使用演示

计算器界面布局

这里写图片描述
(图一)
这里写图片描述
(图二)
使用linearlayout布局实现图一所示计算器布局。

计算器界面布局实现

这里写图片描述
(图三)
在使用线性布局时,先要理解水平布局和垂直布局;水平布局是指控件相互间在空间上都是水平方向对齐,例如图2所示”7”,”8”,”9”,”-“;垂直布局是指控件相互间在空间上都是垂直方向对齐 ,例如图2所示(”7”,”8”,”9”,”-“;)和(”4”,”5”,”6”,”+”;)。
Android LinearLayout支持布局嵌套,所谓布局嵌套就是指在一个布局里面还可以包含其他布局关系,例如在图三中的最后一个黄颜色框布局层里面还嵌套了3个棕颜色布局层。
图三所示计算器布局可分解为:一个垂直布局(绿框)和五个水平布局(黄框)
其中五个水平布局嵌套在垂直布局中;由上往下的第五个黄框中嵌套了垂直布局层和水平布局层,在垂直布局层层中又嵌套了2个水平布局层(”1”“2”“3”)和(”0”“.”)。如图四所示。
这里写图片描述
(图四)

计算器界面布局代码(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="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:gravity="center_vertical|right"
            android:inputType="number"
            android:text="0"
            android:textAlignment="gravity"
            android:textSize="64dp"
            android:textStyle="italic" >

            <requestFocus />
        </EditText>
    </LinearLayout>

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

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="c"
            android:textSize="36dp" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="÷"
            android:textSize="36dp" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="×"
            android:textSize="36dp" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="←"
            android:textSize="36dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7"
            android:textSize="36dp" />

        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="8"
            android:textSize="36dp" />

        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="9"
            android:textSize="36dp" />

        <Button
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="-"
            android:textSize="36dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4"
            android:textSize="36dp" />

        <Button
            android:id="@+id/button10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5"
            android:textSize="36dp" />

        <Button
            android:id="@+id/button11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6"
            android:textSize="36dp" />

        <Button
            android:id="@+id/button12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+"
            android:textSize="36dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2" >

        <LinearLayout
            android:layout_width="268dp"
            android:layout_height="256dp"
            android:orientation="vertical" >

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

                <Button
                    android:id="@+id/button13"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="1"
                    android:textSize="36dp" />

                <Button
                    android:id="@+id/button14"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="2"
                    android:textSize="36dp" />

                <Button
                    android:id="@+id/button15"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="3"
                    android:textSize="36dp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="269dp"
                android:layout_height="match_parent" >

                <Button
                    android:id="@+id/button16"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="0"
                    android:textSize="36dp" />

                <Button
                    android:id="@+id/button17"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="."
                    android:textSize="36dp" />

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="116dp" >

            <Button
                android:id="@+id/button18"
                android:layout_width="88d 
"
                android:layout_height="115dp"
                android:text="="
                android:textSize="36dp" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>


不当之处,请予指正!

猜你喜欢

转载自blog.csdn.net/cqg_blog/article/details/79013446
今日推荐