当前位置:网站首页>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
边栏推荐
- [C language] Analysis of function recursion (3)
- 世界上最大的开源基金会 Apache 是如何运作的?
- 【C语言】细品分支结构——switch语句
- About the development forecast of the market outlook?2021-05-23
- WiFi Association&Omnipeek抓包分析
- hsql是什么_MQL语言
- Selenium本地打开远程浏览器
- ZABBIX配置邮件报警和微信报警
- Mysql index details (with pictures and texts)
- 巴比特 | 元宇宙每日必读:蒂芙尼宣布推出限量版 CryptoPunk 定制吊坠
猜你喜欢

A number of embassies and consulates abroad have issued reminders about travel to China, personal and property safety

二极管及其应用

【Tensorflow】AttributeError: '_TfDeviceCaptureOp' object has no attribute '_set_device_from_string'

苏州大学:从 PostgreSQL 到 TDengine

【Tensorflow】AttributeError: ‘_TfDeviceCaptureOp‘ object has no attribute ‘_set_device_from_string‘

未来的金融服务永远不会停歇,牛市仍将继续 2021-05-28

C language improvement (3)

定了!就在7月30日!

打破文件锁限制,以存储力量助力企业增长新动力

WeChat Mini Program-Recent Dynamic Scrolling Implementation
随机推荐
Mysql index details (with pictures and texts)
shell脚本“画画”
音频处理:浮点型数据流转PCM文件
腾讯安全游戏行业研讨会:生态共建,护航游戏产业健康发展
标量替换、栈上分配、同步消除
社区收藏缓存设计重构实战
泡利不相容原理适用的空间范围(系统)是多大?
Awesome!Alibaba interview reference guide (Songshan version) open source sharing, programmer interview must brush
世界上最大的开源基金会 Apache 是如何运作的?
网络安全第六次作业
网络安全第五次作业
SQL函数 USER
About the development forecast of the market outlook?2021-05-23
如何解决mysql服务无法启动1069
RKMPP库快速上手--(一)RKMPP功能及使用详解
Why does a four-byte float represent a wider range than an eight-byte long
C language improvement (3)
如何解决1045无法登录mysql服务器
C# 编译错误:Compiler Error CS1044
电脑死机,Word忘了保存怎么办?怎么恢复?(编辑器是WPS)