在LinearLayout中使用GridView和Button

3

我尝试在以下代码的线性布局中添加一个按钮在网格视图下方。但是当我执行我的应用程序时,我只看到了网格视图,没有按钮项。因此,这可能是一个基本的错误,但我无法看到它。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:background="#2a2a2a"
         >
            <GridView 
                    android:id="@+id/grid_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:columnWidth="150dp"
                    android:numColumns="auto_fit"
                    android:horizontalSpacing="10dp"
                    android:verticalSpacing="10dp"
                    android:stretchMode="columnWidth"
                    android:gravity="center"></GridView>
            <Button 
                    android:id="@+id/button1" 
                    android:layout_width="wrap_content" 
                    android:layout_height="50dp" 
                    android:text="@string/cam_string" />

    </LinearLayout>
2个回答

6

将您的 LinearLayout 的方向更改为 vertical。我怀疑您看不到按钮是因为 GridView 占据了所有的水平空间,然后 LinearLayout 在屏幕上不可见的位置添加了按钮。您还可以将 GridView 的 layout_height 更改为 "0dip",并给它添加 `android:layout_weight=1' 属性,以便它填充除按钮占用的空间之外的所有空间。


2

除了其他人提出的建议解决方案外,您可以尝试使用相对布局的替代方案。

您可以使用 RelativeLayout 代替 LinearLayout 来实现此目的。

         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#2a2a2a" >

          <GridView 
                android:id="@+id/grid_view"
                android:layout_above="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:columnWidth="150dp"
                android:numColumns="auto_fit"
                android:horizontalSpacing="10dp"
                android:verticalSpacing="10dp"
                android:stretchMode="columnWidth"
                android:gravity="center"></GridView>
           <Button
               android:id="@+id/button1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentBottom="true"
               android:layout_centerHorizontal="true"
               android:text="@string/cam_string" />

          </RelativeLayout>

谢谢,@Raghunandan,它可以工作,但按钮移动到了GridView层。 - cyildirim
我不理解你的评论。 - Raghunandan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接