Java 如何使gridview居中?
我尝试了在其他帖子中找到的解决方案,但在我的代码中似乎没有一个可行。 下面是一段代码: 编辑:已更新,已删除一个RelativeLayout,仍然未居中Java 如何使gridview居中?,java,android,gridview,Java,Android,Gridview,我尝试了在其他帖子中找到的解决方案,但在我的代码中似乎没有一个可行。 下面是一段代码: 编辑:已更新,已删除一个RelativeLayout,仍然未居中 GridView gridView = new GridView(this); HallAdapter hallAdapter = new HallAdapter(this, listOfSeats); gridView.setNumColumns(columnCount); gridView.setAdapter(hallAdapter);
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);
我想我必须添加一些布局参数,但我不知道在哪里。
活动的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">
这就是它现在的样子:
编辑:
我想知道它是这样的吗?还是我错了?
将您的代码修改为
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);
添加视图时使用布局参数
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
relativeLayoutMain.addView(relativeLayoutForZoom, params);
再看你的问题,我有另一个答案。 您是否需要相对缩放
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);
将
zoomView
的布局参数设置为params
,并将规则作为params.addRule(RelativeLayout.CENTER_HORIZONTAL)添加到params中代码>
关于,
<GridView
android:id="@+id/gridView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:numColumns="3" >
</GridView>
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
gridview.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams LayoutParams=
(RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
layoutParams.addRule(父项中的RelativeLayout.CENTER,RelativeLayout.TRUE);
setLayoutParams(layoutParams);
尝试添加
parms.addRule(RelativeLayout.CENTER_IN_PARENT);
连同params.addRule(RelativeLayout.over,R.id.infoTextView)
居中的标准方法是保持高度和宽度的最大值,并将物体定位在最大高度/2和最大宽度/2。您可以尝试setHorizontalGravity
和setVerticalGravity
更改xml属性android:layout\u centerHorizontal=“true”android:layout\u centerVertical=“true”在GridView标签中,安卓:gravity=“center”
@pawkondr你尝试过我的解决方案了吗?我同意,但你选择了哪个对象?到gridview?@pawkondr是到gridview.RelativeLayout.LayoutParams p=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.WRAP_内容);p、 addRule(相对中心水平);设置布局参数(p);我试过了,但它不起作用。@pawkondr实际上它看起来怎么样?还是和begginingI一样我注意到它设置gridView在textfield上面:params.addRule(RelativeLayout.over,R.id.infoTextView);您可以为zoomView添加此参数,如此答案所示。不需要在另一个布局中包装zoomView。你是对的,我删除了传统的相对布局,但仍然没有居中。你的RealitveLayout设置为包装内容。让它和父母匹配,然后你将拥有全宽,zoomView将居中。将RelativeLayout height和width设置为与XML中的父项匹配。它没有改变任何东西问题是我在XML中没有此gridview将你的mainRelativeLayout
width和height设置为match_parent
,然后将zoomView
的布局参数设置为params
并将规则添加到params
中,作为params.addRule(RelativeLayout.CENTER_HORIZONTAL)代码>