2014-04-25 29 views
0

我正在開發一個簡單的android應用程序,我想添加兩個按鈕到網格視圖。一個按鈕應該位於網格視圖的頂部,另一個位於底部。但我有一個問題網格視圖不能正確顯示。請大家幫我解決這個問題,如何添加按鈕到網格視圖? - Android

這是我的xml文件,

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#3b3b3b" > 

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

    <Button android:id="@+id/btnBack" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_margin="10dp" 
     android:text="Back"/> 

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

    </GridView> 

    <Button android:id="@+id/btnCapturePicture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Camera" 
     android:layout_gravity="center" 
     android:layout_marginBottom="10dp"/> 


</LinearLayout> 

這是java文件,

public class HorsePictureAlbum extends Activity{ 

Button btnCapturePicture; 
Button btnBack; 

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.picture_album); 

    GridView gridView = (GridView) findViewById(R.id.grid_view); 

    // Instance of ImageAdapter Class 
    gridView.setAdapter(new ImageAdapter(this)); 

    /** 
    * On Click event for Single Gridview Item 
    * */ 
    gridView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, 
       int position, long id) { 

      // Sending image id to FullScreenActivity 
      Intent i = new Intent(getApplicationContext(), FullImageActivity.class); 
      // passing array index 
      i.putExtra("id", position); 
      startActivity(i); 
     } 
    }); 

    btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture); 
    btnBack = (Button) findViewById(R.id.btnBack); 

    btnBack.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      finish(); 
     } 
    }); 

    btnCapturePicture.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent camara = new Intent(getApplicationContext(), Camera.class); 
      startActivity(camara); 
     } 
    }); 
} 

}

+0

您要設置按鈕出方的網格權?所以你想要兩個按鈕頂部的網格視圖和其他一個在佈局底部,我對 – Sree

+0

是的。我想添加兩個按鈕頂部和底部的網格視圖 – user2881604

+0

嘗試'LinearLayout'內的所有東西,它生病 – Sree

回答

1

喜檢查下面的代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    android:orientation="vertical" > 

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

     <Button 
      android:id="@+id/btnBack" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:layout_margin="10dp" 
      android:text="Back" /> 

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

     <Button 
      android:id="@+id/btnCapturePicture" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginBottom="10dp" 
      android:text="Camera" /> 
    </LinearLayout> 

</LinearLayout> 
0
<?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/up_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Up button" /> 

    <GridView 
     android:id="@+id/gridView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:columnWidth="50dp" 
     android:numColumns="auto_fit" 
     android:stretchMode="columnWidth" 
     android:layout_below="@+id/up_button" 
     android:layout_above="@+id/down_button"> 
    </GridView> 

    <Button 
     android:id="@+id/down_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Down button" 
     android:layout_alignParentBottom="true"/> 

</RelativeLayout> 
相關問題