Android开发基础(学习笔记)2:简单控件与项目

我是菜鸟,刚刚开始学习!如果有不对的,请大佬说明,我虚心学习

简单控件介绍

1. 文本显示

在Android开发中,文本显示是一项基本的功能。我们可以通过文本视图控件来实现。

在XML文件中设置文本:

xml 复制代码
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello, Android!"
    android:textSize="18sp"
    android:textColor="#000000" />

在Java代码中设置文本:

java 复制代码
TextView textView = findViewById(R.id.textView);
textView.setText("Hello, Android!");
textView.setTextSize(18);
textView.setTextColor(Color.BLACK);

引用字符串资源:

在XML文件中引用:

xml 复制代码
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_text" />

在Java代码中引用:

java 复制代码
TextView textView = findViewById(R.id.textView);
textView.setText(getString(R.string.hello_text));

2. 设置文本的大小与颜色

设置文本大小:

在Java代码中:

java 复制代码
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);

在XML文件中:

xml 复制代码
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello, Android!"
    android:textSize="18sp" />

设置文本颜色:

在Java代码中:

java 复制代码
textView.setTextColor(Color.RED);

在XML文件中:

xml 复制代码
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello, Android!"
    android:textColor="#FF0000" />

3. 设置视图的宽高与间距

设置视图宽高:

在Java代码中:

java 复制代码
View view = findViewById(R.id.view);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = 200; // in pixels
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; // or MATCH_PARENT
view.setLayoutParams(layoutParams);

在XML文件中:

xml 复制代码
<View
    android:id="@+id/view"
    android:layout_width="200dp"
    android:layout_height="wrap_content" />

设置视图间距:

使用layout_marginpadding属性。

xml 复制代码
<View
    android:id="@+id/view"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:padding="8dp" />

4. 设置视图的对齐方式

使用layout_gravitygravity属性:

xml 复制代码
<View
    android:id="@+id/view"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:gravity="center" />

常用布局

1. 线性布局(LinearLayout)

基本用法:

xml 复制代码
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <!-- Child views go here -->

</LinearLayout>

2. 相对布局(RelativeLayout)

基本用法:

xml 复制代码
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Child views go here with layout parameters -->

</RelativeLayout>

相对位置属性:

xml 复制代码
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/otherView"
    android:layout_below="@+id/someView"
    <!-- Other attributes -->
    />

3. 网格布局(GridLayout)

基本用法:

xml 复制代码
<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="4"
    android:rowCount="5">

    <!-- Child views go here with layout parameters -->

</GridLayout>

实战项目:简易计算器

1. 整体布局:

xml 复制代码
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Result display -->
    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:gravity="end"
        android:text="0"
        android:textSize="24sp" />

    <!-- Calculator buttons -->
    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:columnCount="4"
        android:rowCount="5">

        <!-- Buttons go here with layout parameters -->

    </GridLayout>

</LinearLayout>

2. 按钮布局:

xml 复制代码
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="1" />

3. 点击事件处理:

java 复制代码
Button button = findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Handle button click
    }
});

4. 计算逻辑处理:

java 复制代码
public class CalculatorActivity extends AppCompatActivity {

    private TextView resultTextView;
    private String currentInput = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        resultTextView = findViewById(R.id.resultTextView);

        // Set onClickListener for each button
        setButtonClickListeners();
    }

    private void setButtonClickListeners() {
        // Handle button clicks
    }

    private void handleButtonClick(String buttonValue) {
        // Handle the logic when a button is clicked
    }

    private void updateDisplay() {
        // Update the display with the current input
        resultTextView.setText(currentInput);
    }
}

以上是一个简单的Android开发入门文章

相关推荐
咩咩啃树皮2 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
鱟鲥鳚2 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
大模型码小白4 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
腾渊信息科技公司4 小时前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
爱笑的源码基地5 小时前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
wuqingshun3141595 小时前
TCP超时重传机制是为了解决什么问题?
java
HLC++5 小时前
Linux的进程间通信
android·linux·服务器
莫逸风8 小时前
【AgentScope 2.0】 0. 学习指南
java·llm·agent·agentscope
z123456789869 小时前
2026最新两款AI编程工具深度对比实测
java·数据库·ai编程
爱笑鱼9 小时前
Binder(二):AIDL 生成的 Proxy、Stub 和 Parcel 到底在做什么?
android