简体   繁体   中英

How to center a gird view in the middle of the screen?

I am having trouble centering a gridview in the middle of the screen. Right now it looks like the screenshot at the bottom: notice the right edge has way more padding than the left one. Tried everything, including placing the gridview in a relativelayout and centering it.

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>

Screenshot of the issue: as you can see there is a lot of blank space on the right. enter image description here

RelativeLayout child view android:layout_centerInParent="true" use

 <?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>
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);
    }

try this way you need to put GridView inside a RelativeLayout and just set GridView's layout_centerInParent to 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>
        **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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM