generateDefaultLayoutParams()

本文深入探讨了ViewGroup及基本布局类中如何通过`generateDefaultLayoutParams`函数生成自定义LayoutParams,旨在简化布局配置过程。通过该函数,开发者无需自行决定使用哪种LayoutParams,从而提高代码效率和减少错误发生。

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

在ViewGroup, 以及几种基本的Layout类中都有这个函数,分别生成自己所定义扩展的LayoutParams, 在某个Layout中需要生成一个LayouParamst的时候,可以方便使用

无需自己根据当前所处Layout的类来确定new哪一种LayoutParams.

/**
     * Returns a set of default layout parameters. These parameters are requested
     * when the View passed to {@link #addView(View)} has no layout parameters
     * already set. If null is returned, an exception is thrown from addView.
     *
     * @return a set of default layout parameters or null
     */
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }


// // 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、付费专栏及课程。

余额充值