如何居中gridview?

时间:2015-12-18 12:28:54

标签: java android gridview

我尝试过在其他帖子中找到的解决方案,但它们似乎都不适用于我的代码。 这是一段代码: 编辑:更新,删除了一个RelativeLayout,仍然没有居中

GridView gridView = new GridView(this);
HallAdapter hallAdapter = new HallAdapter(this, listOfSeats);
gridView.setNumColumns(columnCount);
gridView.setAdapter(hallAdapter);
ZoomView zoomView = new ZoomView(this);
zoomView.addView(gridView);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    params.addRule(RelativeLayout.ABOVE, R.id.infoTextView);

    RelativeLayout relativeLayoutMain = (RelativeLayout)findViewById(R.id.relativeLayout);
    relativeLayoutMain.addView(zoomView, params);

我认为我必须添加一些LayoutParameters,但我不知道在哪里。 活动的XML文件的一部分:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/relativeLayout">

这就是它现在的样子: Screenshot

编辑: 我想知道它是这样的吗?还是我错了? enter image description here

5 个答案:

答案 0 :(得分:0)

将您的代码修改为此

GridView gridView = new GridView(this);
        HallAdapter hallAdapter = new HallAdapter(this, listOfSeats);
        gridView.setNumColumns(columnCount);
        gridView.setAdapter(hallAdapter);
        ZoomView zoomView = new ZoomView(this);
        zoomView.addView(gridView);
        RelativeLayout relativeLayoutForZoom = new RelativeLayout(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ABOVE, R.id.infoTextView);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        relativeLayoutForZoom.addView(zoomView);


        RelativeLayout relativeLayoutMain = (RelativeLayout)findViewById(R.id.relativeLayout);
        relativeLayoutMain.setGravity(Gravity.CENTER);
        relativeLayoutMain.addView(relativeLayoutForZoom,params);

答案 1 :(得分:0)

添加视图时使用布局参数

RelativeLayout.LayoutParams params =
        new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.CENTER_HORIZONTAL);

relativeLayoutMain.addView(relativeLayoutForZoom, params);

答案 2 :(得分:0)

在第二次看你的问题后,我有另一个答案。 你需要relativeLayoutForZoom吗?

GridView gridView = new GridView(this);
HallAdapter hallAdapter = new HallAdapter(this, listOfSeats);
gridView.setNumColumns(columnCount);
gridView.setAdapter(hallAdapter);
ZoomView zoomView = new ZoomView(this);
zoomView.addView(gridView);

RelativeLayout.LayoutParams params =
        new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.ABOVE, R.id.infoTextView);

RelativeLayout relativeLayoutMain = (RelativeLayout)findViewById(R.id.relativeLayout);
relativeLayoutMain.addView(zoomView, params);

答案 3 :(得分:0)

zoomView的布局参数设置为params,并将规则添加到参数params.addRule(RelativeLayout.CENTER_HORIZONTAL);

此致

答案 4 :(得分:0)

尝试添加

parms.addRule(RelativeLayout.CENTER_IN_PARENT);

以及params.addRule(RelativeLayout.ABOVE, R.id.infoTextView);