LayoutParams继承于Android.View.ViewGroup.LayoutParams.

本文详细介绍了LayoutParams的概念及其在不同布局中的应用,包括如何通过LayoutParams控制View的宽度、高度及位置,并提供了具体的代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。
       可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。

       但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:
       1,一个确定的值;
       2,FILL_PARENT,即填满(和父容器一样大小);
       3,WRAP_CONTENT,即包裹住组件就好。

在JAVA中动态构建的布局,常常这样写:

setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

上面这一句话其实是子对父的,也就是说,父布局下的子控件要设置这句话。

因为布局很多,虽然都继承至ViewGroup但是各个布局还是有很大的不同。

很显然上面这句应该这样写才算准确:

setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT));

这表示这个子控件的父布局是一个TableRow , 这样的LayoutParams 太多,所以应明确指明。

 

下面分别说下两个常用到布局:

1. FrameLayout下动态设置子控件居中,动态用JAVA代码要这样实现:

FrameLayout.LayoutParams lytp = new FrameLayout.LayoutParams(80,LayoutParams.WRAP_CONTENT);
lytp .gravity = Gravity.CENTER;
btn.setLayoutParams(lytp);

2. RelativeLayout下动态设置子控件居中:

RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
btn1.setLayoutParams(lp);
// 动态初始化小圆点,便于代码的扩展。
        for (int i = 0; i < mImageIds.length; i++) {
            View point = new View(this);
            point.setBackgroundResource(R.drawable.shape_point_gray);// 所有的View都有背景,
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    10, 10);//宽高为10像素,因为圆点的父控件是LinearLayout所以这里用LinearLayout.LayoutParams,
            if (i > 0) {
                params.leftMargin = 10;// 设置圆点间隔
            }
            point.setLayoutParams(params);// 设置圆点的大小
            llPointGroup.addView(point);// 将圆点动态添加给线性布局
        }

 

// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package android.widget; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.view.ViewDebug.ExportedProperty; public class TableRow extends LinearLayout { public TableRow(Context context) { super((Context)null); throw new RuntimeException("Stub!"); } public TableRow(Context context, AttributeSet attrs) { super((Context)null); throw new RuntimeException("Stub!"); } public void setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener listener) { throw new RuntimeException("Stub!"); } protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { throw new RuntimeException("Stub!"); } protected void onLayout(boolean changed, int l, int t, int r, int b) { throw new RuntimeException("Stub!"); } public View getVirtualChildAt(int i) { throw new RuntimeException("Stub!"); } public int getVirtualChildCount() { throw new RuntimeException("Stub!"); } public LayoutParams generateLayoutParams(AttributeSet attrs) { throw new RuntimeException("Stub!"); } protected LinearLayout.LayoutParams generateDefaultLayoutParams() { throw new RuntimeException("Stub!"); } protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { throw new RuntimeException("Stub!"); } protected LinearLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { throw new RuntimeException("Stub!"); } public CharSequence getAccessibilityClassName() { throw new RuntimeException("Stub!"); } public static class LayoutParams extends LinearLayout.LayoutParams { @ExportedProperty( category = "layout" ) public int column; @ExportedProperty( category = "layout" ) public int span; public LayoutParams(Context c, AttributeSet attrs) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(int w, int h) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(int w, int h, float initWeight) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams() { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(int column) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(ViewGroup.LayoutParams p) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(ViewGroup.MarginLayoutParams source) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) { throw new RuntimeException("Stub!"); } } } 没有setMinWidth
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值