当前位置:网站首页>LayoutParams的详解
LayoutParams的详解
2022-08-02 14:01:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
简述:
LayoutParams继承于Android.View.ViewGroup.LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。
通俗地讲(这里借鉴了网上的一种说法),LayoutParams类是用于child view(子视图)向parent view(父视图)传达自己的意愿的一个东西(孩子想变成什么样向其父亲说明)。举个栗子,子视图和父视图分别可以简单理解成一个LinearLayout 和该LinearLayout里边的一个 TextView 的关系, TextView 就算LinearLayout的子视图 child view。需要注意的是LayoutParams只是ViewGroup的一个内部类这里边这个也就是ViewGroup里边这个LayoutParams类是base class基类实际上每个不同的ViewGroup都有自己的LayoutParams子类。比如AbsListView.LayoutParams, AbsoluteLayout.LayoutParams, Gallery.LayoutParams, ViewGroup.MarginLayoutParams, WindowManager.LayoutParams等。
ViewGroup.LayoutParams类只能简单的设置高height以及宽width两个基本的属性,宽和高都可以设置成三种值:
1,一个确定的值;
2,MATCH_PARENT;
3,WRAP_CONTENT。
ViewGroup.MarginLayoutParams类是ViewGroup.LayoutParams的子类,顾名思义,它只能设置child View的margin属性信息。
1、利用setMargins(left,top,right,bottom);
2、利用MarginLayoutParams对象params方法单独设置.topMargin
基本使用:
常用子类的简单使用:LinearLayout.LayoutParams、RelativeLayout.Params以及WindowManager.Params。
一、LinearLayout.LayoutParams
LinearLayout.LayoutParams的继承关系—>ViewGroup.MarginLayoutParams—>ViewGroup.LayoutParams.从继承关系来看LinearLayout.LayoutParams最少已经可以支持动态设置高度、宽度以及margin属性。
LinearLayout.Params本身自己的属性:gravity和weight属性
基本的使用
1、创建xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lzy.layoutparamsdemo.MainActivity" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
</LinearLayout>
2.在MainActivity中动态设置ImageView的属性
public class MainActivity extends Activity {
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
/**
* @方法描述:
* @author lizhenya
*/
private void initViews() {
img = (ImageView) findViewById(R.id.img);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
img.getLayoutParams());
/**
* 高度和宽度是从ViewGroup.MarginLayoutParams继承而来,ViewGroup.MarginLayoutParams的高度和宽度又是
* 从ViewGroup.Params继承而来,是基本的属性.
*/
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
/**
* margin属性是从ViewGroup.MarginLayoutParams继承而来。
*/
params.setMargins(200, 200, 0, 0);
//LinearLayout.LayoutParams自身的属性
params.gravity = Gravity.CENTER;
params.weight = (float) 1.0;
img.setLayoutParams(params);
}
}
效果图前后的对比
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/125315.html原文链接:https://javaforall.cn
边栏推荐
猜你喜欢
随机推荐
乐心湖‘s Blog——MySQL入门到精通 —— 囊括 MySQL 入门 以及 SQL 语句优化 —— 索引原理 —— 性能分析 —— 存储引擎特点以及选择 —— 面试题
数值的整数次方
拯救流浪猫 | 「喵先锋」系列数字版权盲盒明日开抢
泡利不相容原理适用的空间范围(系统)是多大?
web测试和app测试的区别?
一维卷积神经网络_卷积神经网络的基础知识「建议收藏」
不精确微分/不完全微分(Inexact differential/Imperfect differential)
线程安全问题及关键字synchronized,volatile
k8s之KubeSphere部署有状态数据库中间件服务 mysql、redis、mongo
智能指针-使用、避坑和实现
Gstreamer Plugin注册流程详解
网络安全第三次作业
Awesome!Alibaba interview reference guide (Songshan version) open source sharing, programmer interview must brush
SQL函数 USER
Configure zabbix auto-discovery and auto-registration.
Mysql index details (with pictures and texts)
面试官:可以谈谈乐观锁和悲观锁吗
巴比特 | 元宇宙每日必读:蒂芙尼宣布推出限量版 CryptoPunk 定制吊坠
好用的php空间,推荐国内三个优质的免费PHP空间[通俗易懂]
【ONE·Data || 排序入门】