如何在 GridLayout 中垂直滚动?我已经尝试使用带有 GridLayout 作为子级的 ScrollView,在 GridLayout 中使用 RelativeLayout 子级,但这会导致所有子级堆叠在一起或超出布局。
在 ScrollView 中使用 GridLayout 之前:
在 ScrollView 中的 GridLayout 之后:
我需要将 ScrollView 中的 GridLayout 作为 Child 来实现这种效果
我的 XML 布局文件:
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:fillViewport="true">
<LinearLayout
android:id="@+id/boxContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="315dp">
<!-- RelativeLayout children go here -->
</GridLayout>
</LinearLayout>
</ScrollView>
我在 OnCreate 中使用 LayoutParams 初始化它们:
//MiddleLayout
GridLayout.LayoutParams middleLayoutLayoutParams = (GridLayout.LayoutParams)middleLayout.getLayoutParams();
middleLayoutLayoutParams.setMargins(2, 273, 400, 0);
middleLayoutLayoutParams.setGravity(Gravity.LEFT);
middleLayoutLayoutParams.width = 424;
middleLayout.setLayoutParams(middleLayoutLayoutParams);
middleLayout.setBackgroundDrawable(new ColorDrawable(Color.RED));
middleLayout.bringToFront();
//MiddleLayout1
GridLayout.LayoutParams middleLayoutLayoutParams1 = (GridLayout.LayoutParams)middleLayout1.getLayoutParams();
middleLayoutLayoutParams1.setMargins(431, 273, -2, 0);
middleLayout1.getLayoutParams().width = 645;
middleLayout1.setLayoutParams(middleLayoutLayoutParams1);
middleLayout1.setBackgroundDrawable(new ColorDrawable(Color.GREEN));
//TopLayout
GridLayout.LayoutParams topLayoutLayoutParams = (GridLayout.LayoutParams)topLayout.getLayoutParams();
topLayoutLayoutParams.setMargins(2, 0, -2, 676);
topLayout.getLayoutParams().width = 631;
topLayout.setLayoutParams(topLayoutLayoutParams);
topLayout.setBackgroundDrawable(new ColorDrawable(Color.rgb(255, 154, 0)));
//TopLayout1
GridLayout.LayoutParams topLayoutLayoutParams1 = (GridLayout.LayoutParams)topLayout1.getLayoutParams();
topLayoutLayoutParams1.setMargins(638, 0, -2, 676);
topLayout1.getLayoutParams().width = 440;
topLayout1.setLayoutParams(topLayoutLayoutParams1);
topLayout1.setBackgroundDrawable(new ColorDrawable(Color.BLUE));
//bottomLayout
GridLayout.LayoutParams bottomLayoutLayoutParams = (GridLayout.LayoutParams)bottomLayout.getLayoutParams();
bottomLayoutLayoutParams.setMargins(2, 0, -2, 2);
bottomLayout.getLayoutParams().width = 1073;
bottomLayout.setLayoutParams(bottomLayoutLayoutParams);
bottomLayout.setBackgroundDrawable(new ColorDrawable(Color.rgb(0, 0, 153)));
知道如何让它们在 GridLayout 中正确显示,或以编程方式创建 ScrollView/GridLayout 吗?
谢谢!
最佳答案
我一直在通过设置行和列中的元素来使用 GridLayouts。垂直滚动没有任何问题。你试过这样的事情吗?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="16" >
<TextView
android:id="@+id/orange"
android:layout_row="0"
android:layout_column="0"
android:layout_columnSpan="6"
android:text="Social" />
<Space
android:id="@+id/space_col"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_column="6"
android:layout_columnSpan="4" />
<TextView
android:id="@+id/blue"
android:layout_row="0"
android:layout_column="10"
android:layout_columnSpan="6"
android:text="Utilities" />
<TextView
android:id="@+id/red"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="6"
android:text="Games" />
<TextView
android:id="@+id/green"
android:layout_row="1"
android:layout_column="6"
android:layout_columnSpan="10"
android:text="Google" />
</GridLayout>
</ScrollView>
您可以根据需要通过为 TextView 添加顶部/底部边距来调整行高。不要使用边距和重力来定位子项。仅使用行/列规范。
关于java - Android - 使用 GridLayout 垂直滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25345885/