Andioid开发(5)设置试图的基本属性
对于视图的理解本质上是一个树形的结构(做过简单JavaFX开发的可能了解使用sencebuilder开发的经历,安卓的界面显示理论与其有相似的效果)我们先通过一个页面做了解
代码如下
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/view">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff"
android:paddingTop="480dp"
android:gravity="center"
android:orientation="vertical"
tools:ignore="MissingConstraints"
android:layout_margin="40dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转图片"
tools:ignore="MissingConstraints"></Button>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
对应的显示图片
当前的节点结构是
-
android:paddingTop
:- 用于设置视图内容(文本、图像等)与视图的上边缘之间的内边距(即当前子节点的距离)。
- 可以使用具体的数值(如
10dp
),也可以使用其他尺寸单位。
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="10dp" android:text="Hello, World!" />
-
android:gravity
:- 用于设置视图内容在视图内的对齐方式,例如居中、左对齐、右对齐等(相对于子节点)。
- 常见的取值包括
center
、start
、end
、top
、bottom
等。 - 如果设置为多个值(如
center|bottom
),视图内容将在水平和垂直方向上同时居中和底部对齐。
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, Centered!" /> </LinearLayout>
-
android:orientation
:- 用于设置线性布局(LinearLayout)的方向,可以是水平或垂直。
- 对于水平方向,子视图将水平排列;对于垂直方向,子视图将垂直排列。
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 子视图将垂直排列 --> </LinearLayout>
-
android:layout_margin
:- 用于设置视图与其父布局边缘之间的外边距(与父节点)。
- 可以设置上、下、左、右四个方向的外边距,也可以使用具体的数值或其他尺寸单位。
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Hello, World!" />
这些属性在Android布局中非常常见,通过调整它们的值,你可以灵活地控制试图的位置、对齐方式和外观。在实际应用中,可以根据设计需求和用户体验调整这些属性的值。