Android:week 3总结 Android布局一之相关练习

例一:

LinearLayout实现的效果:    

注意的地方是,LinearLayout嵌套的第一个LinearLayout的android:gravity="right |center",即内容在右边的中间。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tool:context=".MainActivity"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="##F5F5F5">
        <TextView
            android:layout_margin="10sp"
            android:layout_width="match_parent"
            android:layout_height="150sp"
            android:text="1+1"
            android:textSize="50sp"
            android:gravity="right |center"
            android:paddingRight="20sp"
            android:background="#DCDCDC"/>
<!--        字符与上下左右的间距-->
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F5F5F5">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="DEL"
            android:textSize="50sp"
            android:layout_marginRight="10sp"
            android:layout_marginLeft="10sp"
            android:background="#C0C0C0">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F5F5F5">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"
            android:textSize="50sp"
            android:layout_weight="1"
            android:layout_marginLeft="10sp">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:textSize="50sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9"
            android:textSize="50sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="÷"
            android:textSize="50sp"
            android:layout_weight="1"
            android:layout_marginRight="10sp">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F5F5F5">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:textSize="50sp"
            android:layout_weight="1"
            android:layout_marginLeft="10sp">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:textSize="50sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"
            android:textSize="50sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="×"
            android:textSize="50sp"
            android:layout_weight="1"
            android:layout_marginRight="10sp">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F5F5F5">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:textSize="50sp"
            android:layout_weight="1"
            android:layout_marginLeft="10sp">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:textSize="50sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"
            android:textSize="50sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:textSize="50sp"
            android:layout_weight="1"
            android:layout_marginRight="10sp">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F5F5F5">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="."
            android:textSize="50sp"
            android:layout_weight="1"
            android:layout_marginLeft="10sp">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textSize="50sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="="
            android:textSize="50sp"
            android:layout_weight="1"
            android:background="#FF7F50">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:textSize="50sp"
            android:layout_weight="1"
            android:layout_marginRight="10sp">
        </Button>
    </LinearLayout>
</LinearLayout>

RelativeLayout实现效果: 

 

这里需要注意的是text文本添加不能直接添加,需要在strings.xml里面添加。另外,我修改每个按钮的宽度才使没有空余,其他方法还不晓得。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tool:context=".MainActivity">
    <Button
        android:id="@+id/bt1"
        android:layout_width="match_parent"
        android:layout_height="150sp"
        android:text="1+1"
        android:textSize="50sp"
        android:gravity="right|center">
    </Button>
    <Button
        android:id="@+id/bt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/del"
        android:textSize="50sp"
        android:layout_below="@id/bt1">
    </Button>
    <Button
        android:id="@+id/bt31"
        android:layout_marginLeft="5sp"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="7"
        android:textSize="50sp"
        android:layout_below="@id/bt2">
    </Button>
    <Button
        android:id="@+id/bt32"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="8"
        android:textSize="50sp"
        android:layout_below="@id/bt2"
        android:layout_toRightOf="@id/bt31">
    </Button>
    <Button
        android:id="@+id/bt33"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="9"
        android:textSize="50sp"
        android:layout_below="@id/bt2"
        android:layout_toRightOf="@id/bt32">
    </Button>
    <Button
        android:id="@+id/bt34"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="÷"
        android:textSize="50sp"
        android:layout_below="@id/bt2"
        android:layout_toRightOf="@id/bt33">
    </Button>
    <Button
        android:id="@+id/bt41"
        android:layout_marginLeft="5sp"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="4"
        android:textSize="50sp"
        android:layout_below="@id/bt31">
    </Button>
    <Button
        android:id="@+id/bt42"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="5"
        android:textSize="50sp"
        android:layout_below="@id/bt32"
        android:layout_toRightOf="@id/bt41">
    </Button>
    <Button
        android:id="@+id/bt43"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="6"
        android:textSize="50sp"
        android:layout_below="@id/bt33"
        android:layout_toRightOf="@id/bt42">
    </Button>
    <Button
        android:id="@+id/bt44"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="×"
        android:textSize="50sp"
        android:layout_below="@id/bt34"
        android:layout_toRightOf="@id/bt43">
    </Button>
    <Button
        android:id="@+id/bt51"
        android:layout_marginLeft="5sp"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="1"
        android:textSize="50sp"
        android:layout_below="@id/bt41">
    </Button>
    <Button
        android:id="@+id/bt52"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="2"
        android:textSize="50sp"
        android:layout_below="@id/bt42"
        android:layout_toRightOf="@id/bt51">
    </Button>
    <Button
        android:id="@+id/bt53"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="3"
        android:textSize="50sp"
        android:layout_below="@id/bt43"
        android:layout_toRightOf="@id/bt52">
    </Button>
    <Button
        android:id="@+id/bt54"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="-"
        android:textSize="50sp"
        android:layout_below="@id/bt44"
        android:layout_toRightOf="@id/bt53">
    </Button>
    <Button
        android:id="@+id/bt61"
        android:layout_marginLeft="5sp"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="."
        android:textSize="50sp"
        android:layout_below="@id/bt51">
    </Button>
    <Button
        android:id="@+id/bt62"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="50sp"
        android:layout_below="@id/bt52"
        android:layout_toRightOf="@id/bt61">
    </Button>
    <Button
        android:id="@+id/bt63"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="="
        android:textSize="50sp"
        android:layout_below="@id/bt53"
        android:layout_toRightOf="@id/bt62">
    </Button>
    <Button
        android:id="@+id/bt64"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="50sp"
        android:layout_below="@id/bt54"
        android:layout_toRightOf="@id/bt63">
    </Button>
</RelativeLayout>

例二:

LinearLayout实现的效果:

需要特别注意的使最后两行如何使用线性布局:

       如图:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tool:context=".MainActivity"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100sp"
        android:text="24242"
        android:textSize="50sp"
        android:gravity="right|center"
        android:background="#000000"
        android:textColor="#FFFFFF"
        android:layout_margin="10sp">
    </TextView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70sp">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MC"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="M+"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="M-"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MR"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70sp">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="±"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70sp">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70sp">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:textSize="30sp"
            android:layout_weight="1">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="135sp"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="3">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10sp">
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1"
                    android:textSize="30sp"
                    android:layout_weight="1">
                </Button>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2"
                    android:textSize="30sp"
                    android:layout_weight="1">
                </Button>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3"
                    android:textSize="30sp"
                    android:layout_weight="1">
                </Button>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <Button
                    android:layout_width="207dp"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textSize="30sp"></Button>

                <Button
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="."
                    android:textSize="30sp"></Button>
            </LinearLayout>

        </LinearLayout>
        <LinearLayout
            android:layout_width="102sp"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="="
                android:textSize="30sp"></Button>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

另外:

字符一般用sp

orientation在相对布局无用。 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值