当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
机器学习——交叉验证法
rust使用mysql插入数据
Configure zabbix auto-discovery and auto-registration.
Detailed explanation of stored procedures
劲爆!阿里巴巴面试参考指南(嵩山版)开源分享,程序员面试必刷
Supervision strikes again, what about the market outlook?2021-05-22
AWVS工具介绍[通俗易懂]
【ONE·Data || 排序入门】
GTK:Gdk-CRITICAL **: IA__gdk_draw_pixbuf: assertion ‘GDK_IS_DRAWABLE (drawable)’ failed
网络安全第一次作业(2)
软件测试和硬件测试的区别及概念
鲲鹏devkit & boostkit
CSDN(成长一夏竞赛)- 最大数
Fabric.js 动态设置字号大小
FFmpeg AVPacket详解
线代:已知一个特征向量快速求另外两个与之正交的特征向量
Gstreamer Plugin注册流程详解
Mysql 基本操作指南之mysql查询语句
A number of embassies and consulates abroad have issued reminders about travel to China, personal and property safety
线程安全问题及关键字synchronized,volatile