使用三列制作响应式Gridview图像

时间:2015-10-31 16:49:12

标签: java android eclipse android-layout gridview

我使用有三列的GridView创建了一个android应用程序。该应用程序工作正常,但问题是gridview列没有完全填充视图,如下所示。

enter image description here

在html中,我们可以根据分辨率制作100%的响应,如何在android中做出相同的反应

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="60px"
    android:gravity="center"
    android:horizontalSpacing="5px"
    android:numColumns="3"
    android:stretchMode="columnWidth"
    android:verticalSpacing="5px" />

mygrid.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="80px"
        android:layout_height="100px"
        android:background="#444444" >
    </ImageView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="#669966"
        android:textStyle="bold" >
    </TextView>

</LinearLayout>

mygridActivity.java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class mygridActivity extends Activity {

    private GridView g;

    Integer[] img = { R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher };

    String[] labels = { "Android", "Calender", "Sweety's Home", "New Calender",
            "Home", "Star", "GTalk", "Mick", "No Entry" };

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        g = (GridView) findViewById(R.id.gridView1);
        g.setAdapter(new ImageAdapter(this));

        g.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                Toast.makeText(getBaseContext(), "Pic : " + (arg2 + 1), 1)
                        .show();
            }
        });

    }

    public class ImageAdapter extends BaseAdapter {
        Context mGrid;

        public ImageAdapter(Context g) {
            this.mGrid = g;
        }

        public int getCount() {
            return img.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            View view;

            if (convertView == null) {
                view = new View(mGrid);
                LayoutInflater Inf = getLayoutInflater();
                view = Inf.inflate(R.layout.mygrid, null);
            } else {
                view = convertView;
            }

            ImageView iv = (ImageView) view.findViewById(R.id.imageView1);
            TextView tv = (TextView) view.findViewById(R.id.textView1);
            iv.setImageResource(img[position]);
            tv.setText(labels[position]);
            return view;

        }
    }
}

1 个答案:

答案 0 :(得分:2)

请将重力添加到线性布局中的视图中,使其成为中心而不仅仅是将TextView作为中心

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:gravity="center">
 //your code
</LinearLayout>

根据评论进行更新 要了解dip / dp和px之间的区别,您可以查看http://developer.android.com/guide/topics/resources/more-resources.html#Dimensionhttp://developer.android.com/guide/practices/screens_support.html#density-independence来自Alternativley,您可以在dimens.xml中指定宽度和高度,并为移动设备和平板电脑指定值sw720dp / dimens.xml分别
要处理多个屏幕,请按this