常见Layout的LayoutParams总结

本文详细介绍了Android中不同类型的布局参数,包括LayoutParams、MarginLayoutParams、FrameLayout.LayoutParams、LinearLayout.LayoutParams、RelativeLayout.LayoutParams以及TableLayout.LayoutParams等。解释了这些参数如何控制视图的尺寸、位置及对齐方式。

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

参考:
Android MarginLeft与MarginStart的区别
http://blog.youkuaiyun.com/zhufuing/article/details/40181815

常见Layout的LayoutParams总结

http://haking.iteye.com/blog/1182334

LayoutParams

// <-- android.view.ViewGroup.LayoutParams
public static class LayoutParams {
    public static final int MATCH_PARENT = -1;
    public static final int WRAP_CONTENT = -2;

    public int width;
    public int height;
}
// android:layout_height
// Specifies the basic height of the view.
// android:layout_width
// Specifies the basic width of the view.

MarginLayoutParams

//支持margin的View的布局,继承了LayoutParams
// <-- android.view.ViewGroup.LayoutParams
// <-- android.view.ViewGroup.MarginLayoutParams
public static class MarginLayoutParams extends ViewGroup.LayoutParams {
    public int leftMargin;
    public int topMargin;
    public int rightMargin;
    public int bottomMargin;
    //默认相当于leftMargin,用在RTL布局中
    private int startMargin = DEFAULT_MARGIN_RELATIVE;

    //默认相当于rightMargin,用在RTL布局中
    private int endMargin = DEFAULT_MARGIN_RELATIVE;
}
// android:layout_marginBottom
// Specifies extra space on the bottom side of this view.
// android:layout_marginLeft
// Specifies extra space on the left side of this view.
// android:layout_marginRight
// Specifies extra space on the right side of this view.
// android:layout_marginTop
// Specifies extra space on the top side of this view.

FrameLayout.LayoutParams

// <-- android.view.ViewGroup.LayoutParams
// <-- android.view.ViewGroup.MarginLayoutParams
// <-- android.widget.FrameLayout.LayoutParams
public static class LayoutParams extends MarginLayoutParams {
    public int gravity = UNSPECIFIED_GRAVITY;   //也就是Gravity.TOP | Gravity.START

    public LayoutParams(int width, int height, int gravity) {
        super(width, height);
        this.gravity = gravity;
    }
}
// android:layout_gravity            
// Standard gravity constant that a child can supply to its parent
// 注意区别android:gravity, 这个属性是android.view.Gravity,
// FrameLayout.LayoutParams, LinearLayout.LayoutParams和各种常见的View都有android:gravity ;注意RelativeLayout.LayoutParams没有这个属性。

LinearLayout.LayoutParams

// <-- android.view.ViewGroup.LayoutParams
// <-- android.view.ViewGroup.MarginLayoutParams
// <-- android.widget.LinearLayout.LayoutParams
public static class LayoutParams extends ViewGroup.MarginLayoutParams {
    //非0表示可以让View撑大
    public float weight;

    /**
     * Gravity for the view associated with these LayoutParams.
     *
     * @see android.view.Gravity
     */
    @ViewDebug.ExportedProperty(category = "layout", mapping = {
        @ViewDebug.IntToString(from =  -1,                       to = "NONE"),
        @ViewDebug.IntToString(from = Gravity.NO_GRAVITY,        to = "NONE"),
        @ViewDebug.IntToString(from = Gravity.TOP,               to = "TOP"),
        @ViewDebug.IntToString(from = Gravity.BOTTOM,            to = "BOTTOM"),
        @ViewDebug.IntToString(from = Gravity.LEFT,              to = "LEFT"),
        @ViewDebug.IntToString(from = Gravity.RIGHT,             to = "RIGHT"),
        @ViewDebug.IntToString(from = Gravity.START,            to = "START"),
        @ViewDebug.IntToString(from = Gravity.END,             to = "END"),
        @ViewDebug.IntToString(from = Gravity.CENTER_VERTICAL,   to = "CENTER_VERTICAL"),
        @ViewDebug.IntToString(from = Gravity.FILL_VERTICAL,     to = "FILL_VERTICAL"),
        @ViewDebug.IntToString(from = Gravity.CENTER_HORIZONTAL, to = "CENTER_HORIZONTAL"),
        @ViewDebug.IntToString(from = Gravity.FILL_HORIZONTAL,   to = "FILL_HORIZONTAL"),
        @ViewDebug.IntToString(from = Gravity.CENTER,            to = "CENTER"),
        @ViewDebug.IntToString(from = Gravity.FILL,              to = "FILL")
    })
    public int gravity = -1;

    public LayoutParams(int width, int height, float weight) {
        super(width, height);
        this.weight = weight;
    }
}
// android:layout_gravity          
// Standard gravity constant that a child can supply to its parent
// android:layout_weight      

RelativeLayout

// <-- android.view.ViewGroup.LayoutParams
// <-- android.view.ViewGroup.MarginLayoutParams
// <-- android.widget.RelativeLayout.LayoutParams
 
// android:layout_above                            
// Positions the bottom edge of this view above the given anchor view ID. 
// android:layout_alignBaseline                 
// Positions the baseline of this view on the baseline of the given anchor view ID. 
// android:layout_alignBottom                   
// Makes the bottom edge of this view match the bottom edge of the given anchor view ID. 
// android:layout_alignLeft                        
// Makes the left edge of this view match the left edge of the given anchor view ID. 
// android:layout_alignParentBottom          
// If true, makes the bottom edge of this view match the bottom edge of the parent. 
// android:layout_alignParentLeft               
// If true, makes the left edge of this view match the left edge of the parent. 
// android:layout_alignParentRight             
// If true, makes the right edge of this view match the right edge of the parent. 
// android:layout_alignParentTop               
// If true, makes the top edge of this view match the top edge of the parent. 
// android:layout_alignRight                       
// Makes the right edge of this view match the right edge of the given anchor view ID. 
// android:layout_alignTop                         
// Makes the top edge of this view match the top edge of the given anchor view ID. 
// android:layout_alignWithParentIfMissing      
// If set to true, the parent will be used as the anchor when the anchor cannot be be found for layout_toLeftOf, layout_toRightOf, etc. 
// android:layout_below                            
// Positions the top edge of this view below the given anchor view ID. 
// android:layout_centerHorizontal           
// If true, centers this child horizontally within its parent. 
// android:layout_centerInParent              
// If true, centers this child horizontally and vertically within its parent. 
// android:layout_centerVertical               
// If true, centers this child vertically within its parent. 
// android:layout_toLeftOf                         
// Positions the right edge of this view to the left of the given anchor view ID. 
// android:layout_toRightOf                      
// Positions the left edge of this view to the right of the given anchor view ID.

TableLayout
<-- android.view.ViewGroup.LayoutParams
<-- android.view.ViewGroup.MarginLayoutParams
<-- android.widget.LinearLayout.LayoutParams
<-- android.widget.TableLayout.LayoutParams

for (int i = 0;i<4;i++){ if (i % 2 == 0){ currentRow = new LinearLayout(this); currentRow.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ); rowParams.bottomMargin = 16; // 设置行与行之间的间距 container.addView(currentRow, rowParams); } // 创建一个垂直的 LinearLayout 用于包含图片和文字 LinearLayout itemLayout = new LinearLayout(this); itemLayout.setOrientation(LinearLayout.VERTICAL); // 创建 ImageView 并设置图片 ImageView imageView = new ImageView(this); imageView.setImageURI(imageUris2[i]); LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams( 600, // 图片宽度 400 // 图片高度 ); imageView.setLayoutParams(imageParams); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); itemLayout.addView(imageView); // 创建 TextView 并设置文字 TextView textView = new TextView(this); textView.setText(texts2[i]); LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ); textParams.gravity = android.view.Gravity.CENTER_HORIZONTAL; textParams.topMargin = 8; // 设置文字与图片的间距 textView.setLayoutParams(textParams); itemLayout.addView(textView); // 将组合视图添加到当前行 LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ); itemParams.rightMargin = 66; // 设置元素之间的间距 itemParams.bottomMargin = 66; currentRow.addView(itemLayout, itemParams); } 怎么设置图片圆角
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值