首页 > 解决方案 > 添加到 RelativeLayout 时更改 GridView

问题描述

我有这样一个xml文件:

UPD

     <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ProgramsFragment"
android:background="@color/colorPrimaryDark">

        <!-- TODO: Update blank fragment layout -->


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <GridView
                    android:id="@+id/gridView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:numColumns="auto_fit"
                    android:columnWidth="200dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginBottom="16dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:text="@string/your_articles"
                    android:gravity="center_horizontal"
                    android:textStyle="bold"/>
            </LinearLayout>
        </ScrollView>
    </FrameLayout>

我的目标是制作一个可滚动的布局,其中包含 GridView 和 TextView。TextView 应该在 GridView 下方。当我运行我的程序时,GridView 尺寸变小了,我不知道为什么。可能在 textview 中没有什么可以以这种方式改变 GridView 的大小(我认为)。但也许我错了,因为当我删除 TextView、RelativeLayout 和 ScrollView 时一切正常。怎么了?这是我的问题的屏幕:

UPD2

我通过这种方式添加到 GridView:

private final String[] programs = {"Slim body for a four weeks", "Exercises for woman at home", "Effective exercises for biceps at home",
        "Program of effective trainings twice a week", "Training on a horizontal bar for increasing muscle mass",
        "Fitness program for woman: trainings at home at in gym"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,programs);
    GridView gridView = view.findViewById(R.id.gridView);
    gridView.setAdapter(adapter);

UPD3

android:background="@color/colorPrimaryDark"在这个文件中添加了行,结果在屏幕上。据我了解,整个片段占据了布局的这一部分。但为什么?有一些几乎相同的片段,占据了所有的地方。

标签: androidxmlgridview

解决方案


您应该使用linearlayout而不是relativelayout. 此外,您可以使用nestedscrollview。


推荐阅读