Android studio知识点第二弹

一、Android UI 构建基础

  • View 与 ViewGroup:所有 UI 元素都由它们构建。

  • ViewGroup 是容器,可包含 View 或其它 ViewGroup

  • 每个 Android 界面必须有一个 ViewGroup 作为根容器。


二、布局编写方式

1、XML 布局(推荐)
  • 优点:将布局代码与 Java 逻辑分离,结构清晰。

  • 示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用XML布局文件控制UI界面"
        android:textColor="#ff0000"
        android:textSize="18sp"
        android:layout_centerInParent="true"/>
</RelativeLayout>
2.Java 代码编写布局
  • 通过 new 关键字创建 View 或 ViewGroup,动态添加到布局中。


三、常用布局类型

布局类型特点
线性布局(LinearLayout)水平或垂直排列控件
相对布局(RelativeLayout)通过相对定位排列控件
帧布局(FrameLayout)控件叠加显示
表格布局(TableLayout)

以表格形式排列控件

四、布局通用属性

属性说明
android:id设置布局标识
android:layout_width宽度(如 match_parentwrap_content
android:layout_height高度
android:background背景(颜色或图片)
android:layout_margin外边距(与周围元素或边界的距离)
android:padding内边距(布局与内部控件的距离)

五、LinearLayout 特有属性

属性说明
android:orientation排列方向:vertical(垂直)或 horizontal(水平)
android:layout_weight权重,用于分配剩余空间

六、对齐与位置属性

  • android:gravity:控制容器内部内容的对齐方式(如文本在按钮中的位置)。

  • android:layout_gravity:控制控件本身在父容器中的对齐方式。

七、案例:嵌套 LinearLayout

<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:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <!-- 四个文本组件 -->
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <!-- 四个文本组件 -->
    </LinearLayout>

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值