如何将屏幕中心的网格视图居中?

时间:2016-07-08 12:27:25

标签: android android-gridview

我无法在屏幕中间居中使用gridview。现在它看起来像是底部的屏幕截图:注意右边缘比左边缘有更多的填充。尝试了一切,包括将gridview放在relativelayout中并使其居中。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/gridview"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:columnWidth="90dp"
          android:numColumns="auto_fit"
          android:verticalSpacing="16dp"
          android:horizontalSpacing="16dp"
          android:stretchMode="columnWidth"
          android:longClickable="true"
          android:gravity="center"
          android:layout_margin="16dp"
    />

grid_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="90dp"
              android:gravity="center"
              android:orientation="vertical"
              android:minHeight="90dp"
              android:padding="16dp"
              android:background="#EEEEEE">



        <TextView
            android:id="@+id/text_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="test text"
            android:maxLines="2"
            />


</LinearLayout>

问题的屏幕截图:正如您所看到的,右侧有很多空白区域。 enter image description here

4 个答案:

答案 0 :(得分:0)

RelativeLayout子视图android:layout_centerInParent =&#34; true&#34;使用

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="90dp">
    <TextView
        android:id="@+id/text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="text"
        android:maxLines="2"
        />

</RelativeLayout>

答案 1 :(得分:0)

If you want to keep same space then you have to set dynamic property of gridview like this.

  /**
     * Set gridView data according to device width.
     */
    private void InitilizeGridLayout() {
        Resources r = getResources();
        float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, r.getDisplayMetrics());

        columnWidth = (int) ((getScreenWidth() - ((3 + 1) * padding)) / 3);

        gvGallery.setNumColumns(3);
        gvGallery.setColumnWidth(columnWidth);
        gvGallery.setStretchMode(GridView.NO_STRETCH);
        gvGallery.setPadding((int) padding, (int) padding, (int) padding, (int) padding);
        gvGallery.setHorizontalSpacing((int) padding);
        gvGallery.setVerticalSpacing((int) padding);
    }

答案 2 :(得分:0)

尝试这种方式你需要将GridView放在RelativeLayout中,并将GridView的layout_centerInParent设置为true ..

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

    <GridView
            android:id="@+id/grid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="7"
            android:columnWidth="40dp"
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:paddingTop="2dp"
            android:stretchMode="columnWidth"
            android:background="#696969"
            android:layout_centerInParent="true" />

</RelativeLayout>

答案 3 :(得分:0)

        **Please update your activity_main.xml** 

        <?xml version="1.0" encoding="utf-8"?>
            <GridView android:layout_width="wrap_content"
                android:id="@+id/grid1"
                android:layout_height="match_parent"
                android:columnWidth="130dp"
                android:numColumns="auto_fit"
                android:verticalSpacing="16dp"
                android:horizontalSpacing="16dp"
                android:stretchMode="columnWidth"
                android:layout_centerInParent="true"
                android:background="#000000"
                android:longClickable="true"
                android:gravity="center"
                xmlns:android="http://schemas.android.com/apk/res/android"/>


    **grid_item.xml**
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="90dp"
                android:gravity="center"
                android:orientation="vertical"
                android:minHeight="90dp"
                android:padding="16dp"
                android:background="#EEEEEE">



                <TextView
                    android:id="@+id/txtEmpName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:text="test text"
                    android:maxLines="2"
                    />


            </LinearLayout>

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridViewAdapter gridViewAdapter  = new GridViewAdapter(MainActivity.this);
        GridView gridView = (GridView) findViewById(R.id.grid1);
        gridView.setAdapter(gridViewAdapter);
    }
}

public class GridViewAdapter extends BaseAdapter {
    String [] empName = {"Krunal Patel","user11230","Alpa chauhan","loadjang","Aditya Vyas"};
    Activity context;
    public GridViewAdapter(Activity context) {
        this.context = context;

    }

    @Override
    public int getCount() {
        return empName.length;
    }

    @Override
    public Object getItem(int i) {
        return empName[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater inflat = context.getLayoutInflater();

        Holder holder;
        if (view == null) {
            view = inflat.inflate(R.layout.grid_adapter_layout, null, false);
            holder = new Holder();
            holder.txtEmpName = (TextView) view.findViewById(R.id.txtEmpName);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.txtEmpName.setText(empName[i]);

        return view;
    }

    class Holder {
        TextView txtEmpName;
    }
}

[![enter image description here][1]][1]


  [1]: http://i.stack.imgur.com/yvslu.png