0

我正在尝试在relative布局中添加两个按钮gridview

但是当我添加第二个时,button它有时甚至会崩溃。

我的愿望设计是

:-在此处输入图像描述

但它只成功添加了一个按钮。所以我的一键代码是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/delete_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Delete selected image" />



    <GridView
        android:id="@+id/gridView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/delete_button"
        android:layout_alignParentTop="true"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </GridView>

</RelativeLayout> 

然后出去放这个:-

在此处输入图像描述

如何在网格和底部按钮之间添加我的第二个按钮。

4

4 回答 4

4

<Button
    android:id="@+id/delete_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Delete selected image" />

<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" >

</GridView>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/delete_button"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="22dp"
    android:text="SecondButton" />

</RelativeLayout> 

在第一个按钮的顶部和右侧添加第二个按钮

android:layout_above="@+id/delete_button"
android:layout_alignParentRight="true"

在此处输入图像描述

于 2013-05-14T07:05:06.953 回答
2

嘿试试这个代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/delete_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Delete selected image" />

<Button
    android:id="@+id/add_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/delete_button"
    android:layout_alignParentRight="true"
    android:text="add Button" />

<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/add_button"
    android:layout_alignParentTop="true"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" >
</GridView>

</RelativeLayout>

这是屏幕截图

在此处输入图像描述.

于 2013-05-14T07:11:17.503 回答
1

试试这个代码。它的工作

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/delete_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="Delete selected image" />



        <GridView
            android:id="@+id/gridView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/save_button"
            android:layout_alignParentTop="true"
            android:columnWidth="90dp"
            android:gravity="center"
            android:horizontalSpacing="10dp"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" >
        </GridView>
     <Button
            android:id="@+id/save_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_above="@+id/delete_button"
            android:text="Save" />
    </RelativeLayout> 
于 2013-05-14T07:14:21.927 回答
0

如果您的 Gridview 布局包含许多可能超出屏幕视图的元素,我建议您提供一个 scrollView 布局:

所以源代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/border_blue_solid_transparent" >

        <LinearLayout
            android:id="@+id/LinearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <GridView
                android:id="@+id/gridView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:columnWidth="90dp"
                android:gravity="center"
                android:horizontalSpacing="10dp"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:verticalSpacing="10dp" >
            </GridView>

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="Button" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>
于 2013-05-14T08:55:27.750 回答