约束布局 padding与margin区别 设置图标不可见 INVISIBLE与GONE差别

约束布局

官方文档https://developer.android.google.cn/training/constraint-layout?hl=zh-cn

ConstraintLayout使用
引入ConstraintLayout最新依赖,一般AndroidStudio在新建一个项目时,在build.gradle会默认引入该布局的依赖。

implementation 'com.android.support.constraint:constraint-layout:1.1.2'

主要靠app:layout_constraintTop_toTopOf="@id/tv_1" app:layout_constraintLeft_toLeftOf="@id/tv_1"
该标签进行约束
本view上边框相对于tv_1上边框
本view左边框相对于tv_1左边框

<?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">

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:text="Hello World!"
        />
    <ImageView
        android:id="@+id/layoff_1"
        android:src="@drawable/layoff_1"
        android:layout_height="24dp"
        android:layout_width="24dp"
        app:layout_constraintTop_toTopOf="@id/tv_1"
        app:layout_constraintLeft_toLeftOf="@id/tv_1"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="10dp"

        />


</androidx.constraintlayout.widget.ConstraintLayout>

padding与margin区别
margin是指从自身边框到另一个容器边框之间的距离,就是容器外距离。
padding是指自身边框到自身内部另一个容器边框之间的距离,就是容器内距离。

设置图标不可见

java代码设置

 private ImageView monitor_area1;
@Override
    protected void onCreate(Bundle savedInstanceState) {

        //设置图标不可见
        monitor_area1 =  findViewById(R.id.monitor_area1);
        monitor_area1.setVisibility(View.INVISIBLE);

         //点击触发的图标
         ImageView more = (ImageView) findViewById(R.id.more);
         more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isVisible) {
                    isVisible = false;
                    monitor_area1.setVisibility(View.VISIBLE);
                } else {
                    monitor_area1.setVisibility(View.INVISIBLE);
                    isVisible = true;
                }
            }
        });

}

xml中也可直接设置

android:visibility="visible"
android:visibility="invisible"
android:visibility="gone"

INVISIBLE与GONE差别

visible:显示控件,占用空间,可点击
invisible:不显示控件,保留控件所占用的空间,不可点击
gone:隐藏,不保留控件占用的空间,不可点击
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值