android studio瀑布流布局只显示第一行
时间: 2023-12-13 18:34:35 浏览: 33
针对这个问题,可能是由于瀑布流布局中的子 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();
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)