线性布局小实现——计算器的简单布局

本文介绍了如何使用线性布局(LinearLayout)实现计算器的界面布局。首先采用一个垂直方向的LinearLayout,内部包含一个相对布局和另一个垂直LinearLayout。相对布局中设置控件,并探讨了控件属性对布局的影响。接着在下层LinearLayout中添加多个Button。作者强调LinearLayout的垂直和平行属性,理解这一点对布局至关重要。在布局过程中遇到的问题和解决方法也进行了分享,提倡动手实践和尝试不同布局以优化代码和提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本来是想用相对布局来写计算器的布局,然后莫名其妙的变成了线性布局,心塞。

  • 上代码
<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_height="10dp"
        android:layout_width="match_parent"
        android:layout_weight="0.4">

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Calculate布局实现"
            android:textColor="#1FC8F8"
            android:id="@+id/mainTextView1"
            android:textSize="17sp"/>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:hint="请输入计算"
            android:textSize="40sp"
            android:text="计算输出口"/>

    </RelativeLayout>

    <TextView
        android:layout_height="1dp"
        android:layout_width="match_parent"
        android:background="#000000"/>

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

        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_weight="1.0"
            android:gravity="center"
            android:layout_marginTop
### 使用线性布局创建简单计算器应用 #### 创建项目结构 在 Android Studio 中新建一个项目,选择 `Empty Activity` 模板。设置项目的名称和包名。 #### 设计用户界面 打开 `res/layout/activity_main.xml` 文件,使用 XML 定义 UI 组件。为了简化设计,仅采用垂直方向的 `LinearLayout` 来排列各个控件: ```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"> <!-- 显示屏 --> <EditText android:id="@+id/display" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="none" android:textSize="36sp" android:gravity="end|bottom" android:background="#FFFFFF"/> <!-- 数字按钮区 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="4" android:orientation="horizontal"> <Button android:id="@+id/button_7" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="7"/> <Button android:id="@+id/button_8" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="8"/> <Button android:id="@+id/button_9" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="9"/> <Button android:id="@+id/button_divide" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="/"/> </LinearLayout> <!-- 更多行按照上述模式继续添加 --> </LinearLayout> ``` 此部分定义了一个基本框架,包括显示屏区域以及第一排按键。后续可以通过复制粘贴的方式快速完成剩余键位的设计[^1]。 #### 编写逻辑代码 切换到 Java 或 Kotlin 类文件 (`MainActivity.java` / `MainActivity.kt`) ,编写事件处理程序来响应用户的点击动作,并更新显示内容: 对于Java版本: ```java import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private EditText display; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); display = findViewById(R.id.display); // 获取输入框对象 Button button7 = findViewById(R.id.button_7); button7.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { appendToDisplay("7"); } }); // 对其他按钮也做类似的绑定... } private void appendToDisplay(String str){ String currentText = display.getText().toString(); display.setText(currentText + str); } } ``` 对于Kotlin版本: ```kotlin class MainActivity : AppCompatActivity() { lateinit var display: EditText override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) display = findViewById(R.id.display) // 获取输入框对象 val button7 = findViewById<Button>(R.id.button_7).apply{ setOnClickListener { appendToDisplay("7") } } // 对其他按钮也做类似的绑定... } private fun appendToDisplay(str:String){ val currentText = display.text.toString() display.setText("$currentText$str") } } ``` 通过这种方式实现了基础功能——当按下某个数字或运算符时会在屏幕上追加相应的字符[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值