Andioid开发(5)设置试图的基本属性

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>

对应的显示图片
先例图片
当前的节点结构是

  1. 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!" />
    
  2. android:gravity

    • 用于设置视图内容在视图内的对齐方式,例如居中、左对齐、右对齐等(相对于子节点)。
    • 常见的取值包括centerstartendtopbottom等。
    • 如果设置为多个值(如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>
    
  3. android:orientation

    • 用于设置线性布局(LinearLayout)的方向,可以是水平或垂直。
    • 对于水平方向,子视图将水平排列;对于垂直方向,子视图将垂直排列。
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <!-- 子视图将垂直排列 -->
    </LinearLayout>
    
  4. 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布局中非常常见,通过调整它们的值,你可以灵活地控制试图的位置、对齐方式和外观。在实际应用中,可以根据设计需求和用户体验调整这些属性的值。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值