Java Android - 使用 GridLayout 垂直滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25345885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Scrolling Vertically with a GridLayout
提问by Groot
How can I scroll vertically within a GridLayout? I have already tried using a ScrollView with the GridLayout as a Child, with RelativeLayout children within the GridLayout, but that results in all of the children piling on top of each other and or going outside of the layout.
如何在 GridLayout 中垂直滚动?我已经尝试将 ScrollView 与 GridLayout 作为子项一起使用,在 GridLayout 中使用 RelativeLayout 子项,但这会导致所有子项堆积在彼此之上或超出布局。
Before Using the GridLayout in ScrollView:
在 ScrollView 中使用 GridLayout 之前:
After GridLayout in ScrollView:
在 ScrollView GridLayout 之后:
I need to achieve that effect with the GridLayout inside the ScrollView as a Child
我需要使用 ScrollView 中的 GridLayout 作为子项来实现该效果
My XML layout file:
我的 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>
I am initialising them in OnCreate with LayoutParams:
我正在使用 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)));
Any idea how I can get these to display properly within a GridLayout, or create the ScrollView/GridLayout Programatically?
知道如何让这些在 GridLayout 中正确显示,或以编程方式创建 ScrollView/GridLayout 吗?
Thanks!
谢谢!
采纳答案by user3460486
I have been using GridLayouts by setting up the elements in rows and columns. The vertical scroll works without any problems. Have you tried something like this?
我一直通过在行和列中设置元素来使用 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>
You can adjust the row height by adding margins for top/bottom to the text views as needed. Don't use the margins and gravity to position the children. Only use row/column spec.
您可以根据需要通过将顶部/底部的边距添加到文本视图来调整行高。不要使用边距和重力来定位孩子。仅使用行/列规范。
回答by Jim
You probably need a two-way GridView like this:
您可能需要这样的双向 GridView:
https://github.com/jess-anders/two-way-gridview
https://github.com/jess-anders/two-way-gridview
both horizontal and vertical options are supported.
支持水平和垂直选项。