Android 为什么GridLayout没有';你不能填满所有可用的空间吗?
我有一个GridLayout(我通过编程将子项添加到其中) 结果很糟糕,因为GridLayout没有填满所有可用空间 这就是结果: 这是我的XML:Android 为什么GridLayout没有';你不能填满所有可用的空间吗?,android,Android,我有一个GridLayout(我通过编程将子项添加到其中) 结果很糟糕,因为GridLayout没有填满所有可用空间 这就是结果: 这是我的XML: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_heig
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.GridLayout
android:id="@+id/gridlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" >
</android.support.v7.widget.GridLayout>
</HorizontalScrollView>
</ScrollView>
可能是因为缺少
</ScrollView>
最后。我认为您的布局行为一切正常
android:layout\u width=“match\u parent”
设置对于GridLayout(位于HorizontalScrollView中)没有任何效果。插入到GridLayout中的单个子视图确定GridLayout的实际宽度(如果使用垂直滚动视图容器,则高度),该宽度可以大于父视图的屏幕宽度(因此,宽度=匹配\u父设置在此不起作用;对于子视图也是如此)。GridLayout的列和行具有插入的最大子视图的大小(为此列/行指定)
这是一个非常动态的结构。将自动重新计算列数和行数。请记住使用布局_行和布局_列标记子视图,并可能设置所需的大小-遵循上述规则,例如:
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="text"
android:id="@+id/editText1"
android:layout_row="2"
android:layout_column="8" />
因此,通过更改子视图的宽度,可以控制GridLayout的列宽度。您可以学习以下示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:background="#f3f3f3">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#a3ffa3">
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Col 1,Row4"
android:id="@+id/button"
android:layout_gravity="center"
android:layout_row="4"
android:layout_column="1" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start.."
android:layout_column="4"
android:layout_row="8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Col 6, Row 1."
android:id="@+id/button2"
android:layout_row="1"
android:layout_column="6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Col 6, Row 2."
android:id="@+id/button3"
android:layout_row="2"
android:layout_column="6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button4"
android:layout_gravity="center"
android:layout_column="9"
android:layout_row="3" />
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_row="3"
android:layout_column="8" />
<CheckBox
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="@+id/checkBox"
android:layout_row="6"
android:layout_column="7"
android:textColor="#212995" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="@+id/editText"
android:layout_row="5"
android:layout_column="8"
android:textColor="#952a30" />
</GridLayout>
</HorizontalScrollView>
</LinearLayout>
我希望这是有帮助的。致以最良好的祝愿:)
此外:
我发现,上述事实上可能很难通过编程实现。您可能正在考虑将GridLayout更改为GridView或TableLayout,这更易于处理。要了解更多信息,请访问以下网站:
因为网格视图有自己的滚动条。您应该修改您的
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
并添加
android:fillViewPort=“true”
。这将使内容拉伸以填充屏幕(即视口)。添加视图时,请记住首先将右侧的布局参数添加到视图中。TextView reps=new TextView(此);
android.support.v7.widget.GridLayout.LayoutParams repsLayoutParam=(LayoutParams)reps.getLayoutParams();
repsLayoutParam.setGravity(Gravity.FILL_水平| Gravity.FILL_垂直)代码>
这将创建一个放置在网格布局中的文本视图-将重力设置为水平填充和垂直填充将确保它填充布局中的单元格。老实说,我从未在水平滚动视图中看到网格布局,两者都在滚动视图中:为什么需要所有这些滚动?因为事情可能会变大,根据用户输入:-)尝试添加android:fillViewport=“true”
并将android:layout\u width=“match\u parent”
更改为android:layout\u width=“wrap\u content”
。在水平滚动视图中执行这些更改。