addViewInLayout

113 篇文章 0 订阅
87 篇文章 1 订阅
/**
     * Adds a view during layout. This is useful if in your onLayout() method,
     * you need to add more views (as does the list view for example).
     *
     * If index is negative, it means put it at the end of the list.
     *
     * @param child the view to add to the group
     * @param index the index at which the child must be added
     * @param params the layout parameters to associate with the child
     * @param preventRequestLayout if true, calling this method will not trigger a
     *        layout request on child

     * @return true if the child was added, false otherwise
     */
    protected boolean addViewInLayout(View child, int index, LayoutParams params,

            boolean preventRequestLayout) {}


注意这个方法的权限是protected, 适用于自定义的viewGroup, 今天试了一把, 一直调用addViewInLayout()加入view,但是除非调用了

requestLayout(),(试了一下forceLayout + invalidate 都不行)否则view一直显示不出来.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
针对这个问题,可能是由于瀑布流布局中的子 View 没有填满每一列导致的。可以尝试以下几种方法来解决这个问题: 1. 确保每一列的高度都是一致的,这样才能保证子 View 能够填满每一列。 2. 确保每一个子 View 的高度都是确定的,如果高度不确定,可以在代码中动态计算出每一个子 View 的高度。 3. 确保在添加子 View 的时候,使用的是 addViewInLayout() 方法,而不是 addView() 方法。addViewInLayout() 方法可以确保子 View 能够正确地添加到布局中。 4. 确保在添加子 View 的时候,使用的是 makeAndAddView() 方法,而不是 addView() 方法。makeAndAddView() 方法可以确保子 View 能够正确地添加到布局中,并且能够正确地计算出每一个子 View 的位置和大小。 以下是一个简单的瀑布流布局的示例代码,可以参考一下: ```xml <com.example.waterfalllayout.WaterfallLayout android:id="@+id/waterfall_layout" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` ```java public class WaterfallLayout extends ViewGroup { private int mColumnCount = 3; // 列数 private int mColumnGap = 10; // 列之间的间隔 private int mRowGap = 10; // 行之间的间隔 private int[] mColumnHeight; // 每一列的高度 private List<View> mChildViews = new ArrayList<>(); // 所有子 View public WaterfallLayout(Context context) { super(context); init(); } public WaterfallLayout(Context context, AttributeSet attrs) { super(context, attrs); init(); } public WaterfallLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mColumnHeight = new int[mColumnCount]; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int childWidth = (width - mColumnGap * (mColumnCount - 1)) / mColumnCount; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); measureChild(child, MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY), heightMeasureSpec); } int maxHeight = 0; for (int i = 0; i < mColumnCount; i++) { mColumnHeight[i] = 0; } for (int i = 0; i < childCount; i++) { View child = getChildAt(i); int columnIndex = getMinHeightColumnIndex(); int left = columnIndex * (childWidth + mColumnGap); int top = mColumnHeight[columnIndex]; int right = left + childWidth; int bottom = top + child.getMeasuredHeight(); child.layout(left, top, right, bottom); mColumnHeight[columnIndex] = bottom + mRowGap; maxHeight = Math.max(maxHeight, mColumnHeight[columnIndex]); } setMeasuredDimension(width, maxHeight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // do nothing } private int getMinHeightColumnIndex() { int index = 0; int minHeight = mColumnHeight[0]; for (int i = 1; i < mColumnCount; i++) { if (mColumnHeight[i] < minHeight) { index = i; minHeight = mColumnHeight[i]; } } return index; } public void addChildView(View child) { mChildViews.add(child); addViewInLayout(child, -1, generateDefaultLayoutParams(), true); requestLayout(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值