LinearLayout 结合android:layout_weight 计算器界面

重温android的Layout布局之LinearLayout 结合layout_weight 权重比例使用 做计算器界面

**PS:使用layout_weight 权重 用来分配当前控件占剩余空间的大小
使用权重一般把分配该权重方向长度设置为0dp。
比如:orientation=”vertical” 垂直 则设置layout_height=”0dp”
orientation=”horizontal” 水平 则这是layout_width=“0dp”**

效果:这里写图片描述

思路:这里写图片描述
都是通过嵌套LinearLayout+layout_weight实现 多试试也就上手了

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#1000">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="80"
            android:textSize="75sp"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>

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

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


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

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

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:text="MC"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:text="←" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:text="7"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:text="4"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:text="1"/>

                </LinearLayout>

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

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="MR"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="CE"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="8"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="5"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="2"/>
                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="0"/>
            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="MS"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="CE"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="9"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="6"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="."/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="M+"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="±"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="/"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="*"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="-"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="+"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" android:orientation="vertical">
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="M-"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="√"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="%"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1/x"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:text="="/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>
数字按钮从0-9进行初始化,请使用最简单的方法进行初始化<?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:orientation="horizontal" android:layout_height="100dp"> <TextView android:id="@+id/input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:text="" android:textSize="30sp" /> <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="195dp" android:layout_marginTop="50dp" android:text="" android:textSize="30sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="80dp"> <TextView android:id="@+id/C" android:onClick="oneOnClick" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:gravity="center" android:textSize="30sp" android:text="C"/> <!-- android:background="@drawable/selector_gray1"--> <TextView android:id="@+id/zf" android:onClick="oneOnClick" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:gravity="center" android:textSize="30sp" android:text="+/-"/> <!-- android:background="@drawable/selector_gray1"--> <TextView android:id="@+id/mo" andro
03-10
【文件2-7】 activity_main.xml 1 <TableLayout xmlns:android-"http://schemas.android.com/apk/res/android" 2 3 android:layout width="match parent" 4 android:layout height="match parent" 5 android:stretchColumns="*"> 6 <TableRow 7 android:id="@+id/tr_one" 8 style="@style/rowstyle" 9 android:layout_marginTop-"200dp"> 10 <Button 11 style="@style/btnstyle" 12 android:text-"c"/> 13 <Button 14 style="@style/btnstyle" 15 android:text="-"/> 16 <Button 17 style="@style/btnstyle" 18 android:text="+"/> 19 <Button 20 style="@style/btnstyle" 21 android:text-"-"/> 22 </TableRow> 23 <TableRow android:id="@+id/tr_two" 24 25 style="@style/rowstyle"> 26 <Button 27 style="@style/btnstyle" 28 android:text="7"/> <Button style="@style/btnstyle" 31 android:text-"8"/> 32 <Button 33 style="@style/btnstyle" android:text-"9"/> 34 35 <Button 36 style="@style/btnstyle" 37 android:text="x"/> 38 </TableRow> 39 <TableRow 40 android:id="@+id/tr_three" 41 style="@style/rowStyle"> 42 <Button 43 style="@style/btnstyle" 44 android:text-"6"/> 45 <Button 46 style="@style/btnstyle" 47 android:text-"5"/> 48 <Button 49 style="@style/btnstyle" 50 android:text-"4"/> 51 <Button 52 style="@style/btnstyle" 53 android:text="/"/> 54 </TableRow> 55 <TableRow 56 android:id="@+id/tr_four" 57 style="@style/rowStyle"> 58 <Button style="@style/btnstyle" android:text-"1"/> 61 <Button 62 style="@style/btnstyle" 63 android:text="2"/> 64 <Button 65 style="@style/btnstyle" 66 android:text-"3"/> 67 <Button 68 style="@style/btnstyle" 69 android:text-"."/> 70 </TableRow> 71 <TableRow 72 android:id-"@+id/tr_five" 73 style="@style/rowstyle"> 74 <Button 75 style="@style/btnstyle" 76 android:layout_span="2" 77 android:text="0"/> 78 <Button 79 style="@style/btnstyle" 80 android:layout _span-"2" 81 android:text-"-"/> </TableRow> 82</TableLayout>写个类似的计算机代码
最新发布
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值