android布局实例_Android

本篇文章通过实例展示了Android布局的基本使用方法,包括线性布局、相对布局和绝对布局等。还介绍了如何使用约束布局来实现更灵活的界面设计。

Android布局实例:LinearLayout和RelativeLayout

android布局实例_Android
(图片来源网络,侵删)

1、LinearLayout(线性布局)

线性布局是最简单的布局类型,它将子视图按照垂直或水平方向排列。

属性:

orientation:设置子视图的排列方向,可以是vertical(垂直)或horizontal(水平)。

gravity:设置子视图在主轴上的对齐方式,可以是center(居中)、top(顶部)、bottom(底部)、left(左侧)或right(右侧)。

layout_weight:设置子视图在主轴上的权重,用于控制子视图的大小。

示例代码:

android布局实例_Android
(图片来源网络,侵删)
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="文本1" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="文本2" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="文本3" />
</LinearLayout>

2、RelativeLayout(相对布局)

相对布局允许子视图相对于其他子视图或父视图进行定位,它提供了更灵活的布局方式。

属性:

layout_alignParentTop、layout_alignParentBottom、layout_alignParentLeft、layout_alignParentRight:设置子视图与父视图的对齐方式。

layout_toLeftOf、layout_toRightOf、layout_above、layout_below:设置子视图与其他子视图的相对位置。

layout_centerInParent:设置子视图在父视图中的居中对齐。

layout_alignWithParentIfMissing:如果子视图不存在,则将其与父视图对齐。

android布局实例_Android
(图片来源网络,侵删)

layout_centerHorizontal、layout_centerVertical:设置子视图在水平或垂直方向上的居中对齐。

示例代码:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本1" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本2" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本3" />
</RelativeLayout>

下面我将为您提供一个简单的 Android 布局示例,采用介绍形式展示不同的布局元素及其对应的XML代码。

布局类型 描述 XML示例代码
线性布局 LinearLayout 水平或垂直排列子视图
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
相对布局 RelativeLayout 根据彼此位置关系排列
android:layout_width="match_parent"
android:layout_height="wrap_content"
纵向介绍布局 TableRow 在介绍布局内水平排列
android:layout_width="match_parent"
android:layout_height="wrap_content"
网格布局 GridLayout 以网格形式排列视图
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="2"
框布局 FrameLayout 所有子视图都堆叠在一起
android:layout_width="match_parent"
android:layout_height="wrap_content"
绝对布局 AbsoluteLayout 通过坐标定位子视图
android:layout_width="match_parent"
android:layout_height="wrap_content"

以下是各个布局类型中的简单视图元素的示例代码:

视图类型 描述 XML示例代码
文本视图 TextView 显示文本内容
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
按钮视图 Button 可点击的按钮
图像视图 ImageView 显示图像
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image"
编辑文本 EditText 允许用户输入文本
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text"
列表视图 ListView 显示列表项
android:layout_width="match_parent"
android:layout_height="wrap_content"

请注意,这些示例代码仅提供了基本的结构,实际使用时可能需要添加额外的属性和嵌套布局以适应具体的设计需求。

原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/697739.html

(0)
未希的头像未希新媒体运营
上一篇 2024-06-19 13:46
下一篇 2024-06-19 13:51

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购  >>点击进入