如何在GridLayout中垂直滚动?我已经尝试使用ScrollView将GridLayout作为子项,在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>
我正在使用LayoutParams在OnCreate中初始化它们:
//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吗?
谢谢!
答案 0 :(得分:9)
我一直在使用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>
您可以根据需要通过在文本视图的顶部/底部添加边距来调整行高。不要使用边距和重力来定位孩子。仅使用行/列规范。
答案 1 :(得分:-2)