安卓开发学习------2.线性布局(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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainLinearComplexActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="100dp"
        android:textColor="#00E800"
        android:gravity="right"
        android:text="0" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:text="9"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:textColor="#ff0000"
            android:textSize="40dp"
            />
        <Button
            android:text="8"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="7"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="*"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="C"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:text="6"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:textColor="#ff0000"
            android:textSize="40dp"
            />
        <Button
            android:text="5"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="4"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="/"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="back"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="19dp"
            android:layout_weight="1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:orientation="vertical">
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                >
                <Button
                    android:text="3"
                    android:layout_width="80dp"
                    android:layout_height="150dp"
                    android:textColor="#ff0000"
                    android:textSize="40dp"
                    android:layout_weight="1"
                    />
                <Button
                    android:text="2"
                    android:layout_width="80dp"
                    android:layout_height="150dp"
                    android:textColor="#ff0000"
                    android:textSize="40dp"
                    android:layout_weight="1"
                    />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                >
                <Button
                    android:text="0"
                    android:layout_width="80dp"
                    android:layout_height="150dp"
                    android:textColor="#ff0000"
                    android:textSize="40dp"
                    android:layout_weight="1"
                    />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            >
            <Button
                android:text="1"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:textColor="#ff0000"
                android:textSize="40dp"
                android:layout_weight="1"
                />
            <Button
                android:text="."
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:textColor="#ff0000"
                android:textSize="40dp"
                android:layout_weight="1"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            >
            <Button
                android:text="-"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:textColor="#ff0000"
                android:textSize="40dp"
                android:layout_weight="1"
                />
            <Button
                android:text="+"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:textColor="#ff0000"
                android:textSize="40dp"
                android:layout_weight="1"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            >
            <Button
                android:text="="
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:textColor="#ff0000"
                android:textSize="40dp"
                android:layout_weight="1"
                />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

结果:
在这里插入图片描述

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我来为你总结一下 Android “屏幕比例割-线性布局使用”实验小结: Android 应用程序的界面布局可以使用线性布局实现线性布局可以按照水平或垂直方向排列子视图,并且可以使用权重来控制子视图在布局中的比例配。在实现屏幕比例割时,可以使用线性布局实现。 具体步骤如下: 1. 在 XML 布局文件中添 LinearLayout 元素,并设置其 orientation 属性为 horizontal 或 vertical。 ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 子视图 --> </LinearLayout> ``` 2. 在 LinearLayout 元素中添子视图,并设置其 layout_weight 属性来控制其在布局中的比例配。 ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <View android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" /> </LinearLayout> ``` 在这里,我们使用了两个 View 元素来实现屏幕比例割,第一个 View 的 layout_weight 属性设置为 1,第二个 View 的 layout_weight 属性设置为 2。这样,第二个 View 在布局中的高度将是第一个 View 的两倍。 需要注意的是,当 LinearLayout 的 orientation 属性设置为 horizontal 时,layout_weight 属性控制的是子视图在布局中的宽度比例配。 总的来说,使用线性布局实现屏幕比例割非常方便,同时也可以使用其他布局方式来实现,例如相对布局、表格布局等。选择合适的布局方式可以使应用程序的界面更美观和易于使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值