2013-05-14 138 views
0

我想在的relative佈局中添加兩個按鈕。使用gridview添加兩個按鈕

但我當我加入第二button它不工作甚至有時崩潰。

我的願望設計

: - enter image description here

但其成功添加只有一個按鈕。所以我的一個按鈕的代碼是

<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> 

,並走出把這個: -

enter image description here

如何添加網格和底部按鈕之間我的第二個按鈕。

+1

logcat中顯示的異常是什麼? 它是一個ClassCastException?你可能想要清理和重建你的項目? – 2013-05-14 07:04:04

+0

你可以發佈你的xml代碼第二個按鈕添加和崩潰的logcat細節。 – Raghunandan 2013-05-14 07:13:07

回答

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" 

enter image description here

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> 
試試你的第二個按鈕

這裏是截圖

enter image description here

1

試試這個code.Its工作

<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> 
0

如果你的GridView佈局包含了很多元素,然後可以超過屏幕視圖,我勸你提供一個滾動視圖佈局:

所以源代碼波紋管:

<?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>