Android安卓开发知识总结(3-7章)

目录

​​​​​​​​​​​​​​​​​​​​​一、安卓开发中各种布局的理解

​​​​​​​1. 线性布局(LinearLayout)

特点:

适用场景:

2. 约束布局(ConstraintLayout)

特点:

适用场景:

3. 表格布局(TableLayout)

特点:

适用场景:

4. 帧布局(FrameLayout)

特点:

适用场景:

5. 相对布局(RelativeLayout)

适用场景:

二、UI界面交互功能的实现方法

1.​​​​​​​ 按钮点击事件

实现方法:

反思与改进:

2. 列表项点击事件​​​​​​​

实现方法:

反思与改进:

3. 滑动操作

实现方法:

反思与改进:

4. 菜单项和对话框

实现方法:

​​​​​​​反思与改进:


​​​​​​​​​​​​​​​​​​​​​一、安卓开发中各种布局的理解

在安卓开发中,布局(Layout)是UI设计的基础,决定了应用程序的界面结构。常见的布局有线性布局、约束布局、表格布局、帧布局和相对布局。下面我将详细介绍每种布局的特点、适用场景以及如何在实际项目中应用这些布局。

​​​​​​​1. 线性布局(LinearLayout)


特点:

各个子元素彼此连接,中间不留空白;线性布局可以将子视图按垂直或水平方向排列。
使用 android:orientation 属性可以设置线性布局的排列方向(垂直或水平)。
每个子视图可以使用 layout_weight 属性来分配剩余空间。


适用场景:

当需要简单地将子视图按顺序排列时,使用线性布局是最方便的选择。适合用于垂直或水平的按钮排布、简单的表单等。

示例代码:

<LinearLayout

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" />
</LinearLayout>

2. 约束布局(ConstraintLayout)


特点:

约束布局是一个功能十分丰富和强大的布局,它可以通过设置约束来灵活地摆放子视图,
也可以减少嵌套布局,提高性能。可以在Android Studio中通过拖拽和设置约束来设计界面。


适用场景:

当需要复杂布局或希望减少嵌套布局时,约束布局是最佳选择。适用于需要灵活控制视图位置的界面,如动态调整视图位置的界面。


示例代码:​​​​​​​

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello World"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Click Me"
        app:layout_constraintTop_toBottomOf="@id/textView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
</ConstraintLayout>


3. 表格布局(TableLayout)


特点:

表格布局将子视图按行和列排列,类似于HTML的表格。
使用 TableRow 元素定义行,每个 TableRow 中可以包含多个视图。


适用场景:

适合用于需要按行和列组织视图的场景,比如日历、数据表格等。


示例代码:

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

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 1, Col 1" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 1, Col 2" />
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 2, Col 1" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 2, Col 2" />
    </TableRow>
</TableLayout>

​​​​​​​


4. 帧布局(FrameLayout)


特点:

帧布局简单地将所有子视图堆叠在一起,后添加的视图会覆盖在前面的视图之上。
适用于显示单个子视图或需要重叠视图的场景。


适用场景:

用于需要一个简单的容器来显示一个视图或需要在视图上叠加其他视图的情况,例如相机预览界面。


示例代码:

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/background" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Overlay Text"
        android:layout_gravity="center" />
</FrameLayout>

​​​​​​​

5. 相对布局(RelativeLayout)


特点:

相对布局允许子视图相对于父容器或其他子视图的位置进行定位。
可以通过 layout_alignParent、layout_toLeftOf 等属性来控制视图的位置。


适用场景:

当需要相对定位视图时,相对布局是一个不错的选择。适用于需要相对定位的复杂布局,比如按钮相对于文本视图的排列。


示例代码:

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:layout_centerInParent="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:layout_below="@id/textView"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

​​​​​​​


二、UI界面交互功能的实现方法

在《APP应用开发》课程中,我学习了各种UI界面交互功能的实现方法,如按钮点击事件、列表项点击事件、滑动操作、菜单项和对话框等。下面总结一些具体案例,并反思学习过程。

1.​​​​​​​ 按钮点击事件


实现方法:

使用 setOnClickListener 方法为按钮设置点击事件监听器。
在监听器中实现点击逻辑。

示例代码:

Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
    }
});


反思与改进:

通过查阅官方文档和示例代码,理解了事件监听机制。
参与讨论和分享学习心得,提升了对事件处理的理解。


2. 列表项点击事件​​​​​​​


实现方法:

为 ListView 或 RecyclerView 设置点击事件监听器。
在监听器中处理列表项点击事件。

示例代码:

ListView listView = findViewById(R.id.listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String item = (String) parent.getItemAtPosition(position);
        Toast.makeText(MainActivity.this, "Clicked: " + item, Toast.LENGTH_SHORT).show();
    }
});


反思与改进:

学习了如何处理复杂的列表项点击事件。
通过实验和调试,优化了点击事件的响应速度。


3. 滑动操作


实现方法:

使用 ViewPager 实现滑动切换界面。
使用手势检测器处理自定义滑动操作。


示例代码:

ViewPager viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));


反思与改进:

通过实践,掌握了滑动操作的基本实现方法。
借助开源库和社区资源,学习了更多高级滑动效果的实现技巧。


4. 菜单项和对话框


实现方法:

使用 onCreateOptionsMenu 和 onOptionsItemSelected 方法实现菜单项。
使用 AlertDialog 创建和显示对话框。


示例代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            showSettingsDialog();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

private void showSettingsDialog() {
    new AlertDialog.Builder(this)
        .setTitle("Settings")
        .setMessage("Settings dialog")
        .setPositiveButton("OK", null)
        .show();
}

​​​​​​​
反思与改进:

通过文档学习和代码示例,掌握了菜单项和对话框的基本用法。
通过参与项目实践,提高了对话框的设计和用户体验。


 

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值