ConstraintLayout 实现多个控件居中显示

目标:在不用父布局嵌套的情况下,在ConstraintLayout 包裹下直接实现多个控件(捆绑)控件居中显示

在 ConstraintLayout 1.1.3上,先用代码实现,后有实现思路

<android.support.constraint.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"
    android:background="@color/white"
    tools:context=".ui.ConstrainLayoutActivity">


    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintRight_toLeftOf="@+id/tv"
        />

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈哈"
        android:textColor="@color/white"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@color/square_five"

        app:layout_constraintLeft_toRightOf="@id/imageView"
        />

</android.support.constraint.ConstraintLayout>

实现效果

 

实现思路是在链头加上 类似代码,让脑袋中抽象的链条 用constraintLayout的chain属性实现

       app:layout_constraintHorizontal_chainStyle="packed"

chain 除了链条方向有横向和竖向区分外, chain链条上的模式有 3种

   * spread - 元素将被展开(默认样式) 。加权链 - 在spread模式下,如果某些小部件设置为MATCH_CONSTRAINT,则它们将拆分可用空间 
   * spread_inside - 类似,但链的端点将不会扩展 
   * packed - 链的元素将被打包在一起。 孩子的水平或垂直偏差属性将影响包装元素的定位

 


 

### 回答1: 好的,下面是一个使用XML编写ConstraintLayout布局的示例,来实现车辆健康应用的界面: ``` <?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" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 定义顶部的标题栏 --> <TextView android:id="@+id/title_text" android:layout_width="0dp" android:layout_height="wrap_content" android:text="车辆健康" android:textSize="24sp" android:textStyle="bold" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- 定义一个ScrollView,用于显示内容 --> <ScrollView android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/title_text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 定义一个显示车辆状态的卡片 --> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardElevation="4dp" app:cardCornerRadius="8dp" android:layout_margin="16dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="车辆状态" android:textStyle="bold" android:textSize="18sp" android:layout_marginBottom="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正常" android:textSize="14sp"/> </LinearLayout> </androidx.cardview.widget.CardView> <!-- 定义一个显示车辆信息的卡片 --> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardElevation="4dp" app:cardCornerRadius="8dp" android:layout_margin="16dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="车辆信息" android:textStyle="bold" android:textSize="18sp" android:layout_marginBottom="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="车型:XXXX" android:textSize="14sp"/> <TextView android:layout_width="wrap_content" android:layout ### 回答2: ConstraintLayout是一种用于Android布局的XML编写方式。通过使用ConstraintLayout,可以实现车辆健康应用界面的设计。在XML布局文件中,可以使用不同的属性和约束来定义各个UI元素之间的关系和位置。 首先,我们可以使用ConstraintLayout的根标签来包裹整个布局。接下来,使用子标签来定义各个UI元素,例如TextView、ImageView和Button等。通过设置它们的id属性,我们可以在XML中引用这些元素。例如: ```xml <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/titleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="车辆健康应用" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:textSize="24sp"/> <ImageView android:id="@+id/carImageView" android:layout_width="200dp" android:layout_height="200dp" android:src="@drawable/car_icon" app:layout_constraintTop_toBottomOf="@+id/titleTextView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"/> <Button android:id="@+id/startButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始检查" app:layout_constraintTop_toBottomOf="@+id/carImageView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"/> </androidx.constraintlayout.widget.ConstraintLayout> ``` 在上述的XML布局代码中,我们使用约束将TextView、ImageView和Button元素垂直排列,并分别与父布局和其他元素进行了约束关系的设置。通过使用约束,可以灵活地控制各个UI元素的位置和大小。 以上是使用ConstraintLayout编写车辆健康应用界面的简单示例。通过深入了解ConstraintLayout的属性和用法,还可以实现更复杂和精确的界面设计。 ### 回答3: 车辆健康应用界面是一种用于显示车辆健康信息的界面,我们可以通过编写ConstraintLayout布局来实现。 首先,我们需要在XML文件中使用ConstraintLayout作为根布局。然后,我们可以使用各种不同类型的布局控件来构建界面,如TextView、ImageView、Button等。 例如,我们可以使用TextView来显示车辆的速度、里程数等基本信息。我们可以在XML中设置TextView的id,然后通过设置TextView的属性来修改显示的文本内容、字体大小、颜色等。 此外,我们还可以使用ImageView来显示车辆的照片或健康状态图标。我们可以在XML中设置ImageView的id,并使用drawable资源或URL来设置图像。 此外,我们还可以使用Button来添加功能按钮,如开始诊断、查看报告等。通过设置Button的属性,我们可以定义按钮的样式、文本内容和背景颜色。 在ConstraintLayout中,我们可以使用约束来定义控件之间的相对位置。通过设置控件的上、下、左、右约束,我们可以将它们放置在屏幕的特定位置。例如,我们可以将TextView放置在屏幕顶部,并将ImageView放置在TextView的下方,以显示车辆信息和照片。 最后,我们可以使用XML中的其他属性来调整界面的外观和行为,如设置背景颜色、间距、边框等。 总之,通过编写ConstraintLayout布局,我们可以灵活地设计和构建车辆健康应用界面,以满足用户的需求和要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值