如何在网格视图中添加按钮? - Android

时间:2014-04-25 08:48:54

标签: android gridview

我正在开发一个简单的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);
        }
    });
}

}

2 个答案:

答案 0 :(得分:2)

嗨检查以下代码:

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

答案 1 :(得分: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>