ConstraintLayout 用法就是这么简单

本文详细介绍ConstraintLayout的使用方法及特点,通过实例演示如何利用其进行高效布局,并附带介绍如何实现元素的位置动画。

1 .ConstraintLayout 用法:

有个刚刚入门Android的小家伙问我说ConstraintLayout中left ,right,top,bottom都是一些什,感觉比较绕,都是啥和啥呀~

为了告诉他是啥,简单写了一个, 写都写了,顺便丢上来吧~

来来来,下面布局基本揽括你写Android能用到的大部分布局, 为什么用ConstraintLayout?
很简单,看下面代码.是不是瀑布流一样,没有任何包裹?这就是它最大优点,大大提升绘制效率有么有~

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_main"
        tools:context=".MainActivity">


    <ImageView
            android:id="@+id/iv"
            android:layout_width="200dp"
            android:layout_height="134dp"
            android:background="@color/colorAccent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
    />

    <TextView
            android:id="@+id/tv1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="杭州市凯旋路x号xx幢100单元x室住宅用途房地产"
            app:layout_constraintLeft_toRightOf="@id/iv"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:paddingLeft="4dp"
            android:textSize="13sp"
            android:paddingTop="10dp"
    />

    <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/tv1"
            android:text="RMB"
            app:layout_constraintLeft_toRightOf="@id/iv"
            android:paddingLeft="4dp"
            android:paddingTop="9dp"
            android:textSize="13sp"
    />

    <TextView
            android:id="@+id/tv3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="当前价"
            android:textSize="10sp"
            android:paddingLeft="4dp"
            app:layout_constraintLeft_toRightOf="@id/iv"
            app:layout_constraintTop_toBottomOf="@+id/tv2"
    />

    <TextView
            android:id="@+id/tv4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="184.5万"
            android:textSize="26sp"
            app:layout_constraintLeft_toRightOf="@id/tv3"
            app:layout_constraintBottom_toBottomOf="@id/tv3"
            android:textColor="@color/colorAccent"
            app:layout_constraintBaseline_toBaselineOf="@id/tv3"
    />
    <TextView
            android:id="@+id/tv5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="出价"
            android:textSize="12sp"
            app:layout_constraintTop_toBottomOf="@id/tv3"
            app:layout_constraintLeft_toRightOf="@id/iv"
            android:paddingLeft="4dp"
            android:paddingTop="12dp"/>
    <TextView
            android:id="@+id/tv6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="89"
            android:textSize="12sp"
            app:layout_constraintTop_toBottomOf="@id/tv3"
            app:layout_constraintLeft_toRightOf="@id/tv5"
            android:paddingLeft="4dp"
            android:paddingTop="12dp"/>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
              app:layout_constraintTop_toBottomOf="@id/tv6"
              app:layout_constraintLeft_toRightOf="@id/iv"
              android:textSize="12sp"
              android:layout_marginLeft="4dp"
              android:paddingTop="4dp"
              android:text="预计明天10:00结束"/>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
              app:layout_constraintTop_toBottomOf="@id/tv5"
              app:layout_constraintRight_toRightOf="parent"
              android:paddingRight="10dp"
              android:text="8.2km"/>

    <Button android:layout_width="100dp" android:layout_height="35dp"
            app:layout_constraintTop_toBottomOf="@id/iv"
            app:layout_constraintLeft_toLeftOf="parent"
            android:background="@drawable/tab_bg"
            android:textColor="@android:color/black"
            android:id="@+id/bt1"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintRight_toLeftOf="@id/bt2"
            android:layout_marginTop="20dp"
            android:text="可贷款"/>

    <Button android:layout_width="100dp" android:layout_height="35dp"
            app:layout_constraintTop_toBottomOf="@id/iv"
            android:background="@drawable/tab_bg"
            android:textColor="@android:color/black"
            android:id="@+id/bt2"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintLeft_toRightOf="@id/bt1"
            app:layout_constraintRight_toLeftOf="@id/bt3"
            android:layout_marginTop="20dp"
            android:text="不限购"/>

    <Button android:layout_width="100dp" android:layout_height="35dp"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintTop_toBottomOf="@id/iv"
            android:background="@drawable/tab_bg"
            android:id="@+id/bt3"
            android:textColor="@android:color/black"
            app:layout_constraintLeft_toRightOf="@id/bt2"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginTop="20dp"
            android:text="特价房"/>


    <TextView
            android:id="@+id/tab1"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:text="tab1"
            android:gravity="center"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/tab2"/>

    <TextView
            android:id="@+id/tab2"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:text="tab2"
            android:gravity="center"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@id/tab1"
            app:layout_constraintRight_toLeftOf="@+id/tab3"
    />

    <TextView
            android:id="@+id/tab3"
            android:background="@drawable/tab_bg"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:gravity="center"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintLeft_toRightOf="@+id/tab2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:text="tab3"/>


</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述

属性

如图:

 <ImageView
            android:id="@+id/iv"
            ...
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
    />

简单理解:

app:layout_constraintTop_toTopOf=“parent” — iv的顶部相对父控件顶部保持
app:layout_constraintLeft_toLeftOf=“parent” — iv的左边相对父控件左边保持

对应属性,控制一个控件: 左右上下 位置

 app:layout_constraintLeft_toLeftOf
 app:layout_constraintTop_toTopOf
 app:layout_constraintBottom_toBottomOf
 app:layout_constraintRight_toRightOf

其他差不多:

layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
用一个图就明白了

自身左/右/上/下 对于某组件 右/左/下/上 或者 左/右/上/下 位置:
以左右位置对比:
在这里插入图片描述

字体大小不一致,又想保持在一个Baseline 如图

对应:

app:layout_constraintBaseline_toBaselineOf="@id/tv3"

在这里插入图片描述

2.上升动画,缩小动画

实现效果mBg2为上升动画,下面布局依然填充下面所有位置

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="people.demo.MainActivity">

    <ImageView
        android:id="@+id/mBg2"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/colorAccent" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/colorPrimary" />

</LinearLayout>

代码

//dp改变成像素
float oldHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 330, getActivity().getResources().getDisplayMetrics());

float newHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getActivity().getResources().getDisplayMetrics());

final ValueAnimator valueAnimator = ValueAnimator.ofFloat(oldHeight, newHeight);

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float value = (float) valueAnimator.getAnimatedValue();
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mBg2.getLayoutParams();
layoutParams.height = (int) value;
mBg2.setLayoutParams(layoutParams);
}
});

valueAnimator.setDuration(500);

valueAnimator.start();
### 使用 ConstraintLayoutAndroid Studio 中实现瀑布流布局 #### 瀑布流布局简介 瀑布流是一种常见的页面布局方式,通常用于展示图片或其他内容项。它通过多列排列的方式,在不同屏幕宽度下自适应显示多个项目[^1]。 #### ConstraintLayout 的优势 `ConstraintLayout` 是一种强大的布局管理器,允许开发者灵活定义视图之间的约束关系。相比传统的 `LinearLayout` 或 `RelativeLayout`,它可以更高效地处理复杂布局结构,并支持动态调整视图位置和大小。 --- #### 实现瀑布流布局的关键步骤 以下是使用 `ConstraintLayout` 和其他辅助组件来实现瀑布流效果的方法: ##### 1. 添加依赖库 为了简化瀑布流的实现过程,可以引入第三方库如 **FlexboxLayout** 来增强功能。在项目的 `build.gradle` 文件中添加以下依赖: ```gradle implementation 'com.google.android.flexbox:flexbox:3.0.0' ``` 如果仅使用原生方法,则无需额外依赖。 ##### 2. 创建 XML 布局文件 创建一个新的布局文件并设置根节点为 `androidx.constraintlayout.widget.ConstraintLayout`。下面是一个简单的例子: ```xml <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Flexbox 容器 --> <com.google.android.flexbox.FlexboxLayout android:id="@+id/flexbox_layout" android:layout_width="0dp" android:layout_height="wrap_content" app:alignContent="stretch" app:alignItems="center" app:flexWrap="wrap" app:justifyContent="space_between" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout> ``` 上述代码片段展示了如何利用 `FlexboxLayout` 配合 `ConstraintLayout` 构建基础框架。 ##### 3. 动态加载数据 在 Java/Kotlin 文件中初始化 `FlexboxLayout` 并填充子视图。例如: ```java import com.google.android.flexbox.FlexboxLayout; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FlexboxLayout flexboxLayout = findViewById(R.id.flexbox_layout); // 循环添加子视图 for (int i = 0; i < 20; i++) { TextView textView = new TextView(this); textView.setText("Item " + i); textView.setPadding(8, 8, 8, 8); // 设置内边距 int randomHeight = getRandomHeight(); // 获取随机高度模拟不规则尺寸 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, randomHeight ); textView.setLayoutParams(params); flexboxLayout.addView(textView); } } private int getRandomHeight() { Random rand = new Random(); return rand.nextInt(150) + 50; // 返回范围内的随机数作为高度 } } ``` 此部分实现了动态生成具有不同高度的子视图,并将其添加到 `FlexboxLayout` 中。 ##### 4. 调整样式与优化性能 可以通过修改属性进一步定制外观,比如间距、对齐方式等。同时注意避免过多嵌套层次以提升渲染效率。 --- ### 注意事项 - 如果完全依靠纯 `ConstraintLayout` 实现复杂的瀑布流逻辑会非常困难且低效,因此推荐结合专门设计的控件(如 `FlexboxLayout`)完成任务。 - 测试过程中需考虑多种设备分辨率下的表现一致性问题。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值