ConstraintLayout系列:ConstraintLayout会自动均分布局(一)

  • 设计稿如下:
这个布局,在不同宽度的屏幕上,都需要左右均分,用LinearLayout写,需要两层LinearLayout,外层水平从左到右排列,内层垂直从上到下。如果用RelativeLayout很难实现这种均分的效果。用ConstraintLayout,就比较方便了,而且只有一层布局。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
            <TextView
                android:id="@+id/tv_wrong_sm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="[sat]"
                android:textStyle="bold"
                android:layout_marginTop="20dp"
                android:textColor="#ff383838"
                android:gravity="center"
                android:textSize="25sp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/tv_right_sm"
                />

            <ImageView
                android:id="@+id/iv_wrong"
                app:layout_constraintTop_toBottomOf="@id/tv_wrong_sm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/icon_wrong_sound"
                android:layout_marginTop="16dp"
                app:layout_constraintLeft_toLeftOf="@id/tv_wrong_sm"
                app:layout_constraintRight_toRightOf="@id/tv_wrong_sm"

                />


            <TextView
                android:id="@+id/tv_right_sm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:text="[seit]"
                android:textStyle="bold"
                android:layout_marginTop="20dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toRightOf="@id/tv_wrong_sm"
                android:textColor="#ff383838"
                android:gravity="center"
                android:textSize="25sp"
                />

            <ImageView
                app:layout_constraintTop_toBottomOf="@id/tv_right_sm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/icon_right_sound"
                android:layout_marginTop="16dp"
                app:layout_constraintLeft_toLeftOf="@id/tv_right_sm"
                app:layout_constraintRight_toRightOf="@id/tv_right_sm"
                />
    
</androidx.constraintlayout.widget.ConstraintLayout>

左侧隐藏,只需要:

 findViewById(R.id.tv_wrong_sm).setVisibility(View.GONE);
 findViewById(R.id.iv_wrong).setVisibility(View.GONE);

隐藏后仍然是居中效果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangjin1120

可靠的文章费时费力,希望支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值