首页 > 技术文章 > 《Android 编程权威指南》学习笔记 : 第10章 使用布局与部件创建用户

easy5weikai 2022-05-29 08:44 原文

《Android 编程权威指南》学习笔记 : 第10章 使用布局与部件创建用户

布局 ConstraintLayout

本节主要是学习使用 【Design】界面来设计布局和添加组件和 ConstraintLayout布局中的约束

ImageView的约束

crime tile 的约束:

crime date 的约束:

在ImageView的属性框中,修改部件的Id为 :crime_solved,提示已经存在同名的资源(在 CrimeFragment 的布局文件中已经定义过同名的资源Id),点击确定即可,不会有问题!
只有在同一布局里,系统才会要求所有部件都使用唯一的ID

修改字体的大小:android:textSize="18sp",单位 sp

代码清单:res/layout/list_item_crime.xml

<?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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/crime_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:text="Crime title"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@+id/crime_solved"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/crime_date"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="Crime date"
        app:layout_constraintEnd_toStartOf="@+id/crime_solved"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/crime_title" />

    <ImageView
        android:id="@+id/crime_solved"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/crime_title"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_solved" />

</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读