Java 在Android中创建gridView中心
我一直在绞尽脑汁研究如何使gridView中心水平和垂直。我还附上了我的屏幕截图。 下面是我的图像代码Java 在Android中创建gridView中心,java,android,Java,Android,我一直在绞尽脑汁研究如何使gridView中心水平和垂直。我还附上了我的屏幕截图。 下面是我的图像代码 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_pare
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#FFFFFF"
android:layout_gravity="center_horizontal"
android:padding="5dip" >
<GridView
android:id="@+id/homeGridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="160dip"
android:adjustViewBounds="true"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:numColumns="2"
android:stretchMode="none"/>
</RelativeLayout>
在你的gridview中使用这两个标签,看看它是否有效
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:padding="5dip" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true">
</ImageView>
</RelativeLayout>
如果您不想使用TableLayout(),那么处理GridView是相当复杂的(我在这方面已经浪费了很多时间)。 我唯一的想法是强迫你的GridView有一个精确的宽度,那就是两个视图的总和:160dip*2
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
可以随意添加10滴或更多的填充物
N.B.:我不确定这是否是最好的方法,也不确定这是否适合每种设备。试试看,让我知道;) 你试过->android:layout\u gravity=“center”是的,我试过@Santhoshit完全没问题……如果它仍然不起作用,试着从你的代码中删除它
android:orientation=“horizontal”
或者,如果您愿意,您可以将父布局改为线性布局。您只需在Gridview和relativelayout中删除重力标记和centerInParent标记,并在Gridview中仅使用这两个标记,请参见……我也尝试了上述方法,但没有做任何更改。。。快速提问:我的图像大小是150dp,列宽是150dp还是150dpmore@theJava您可以保留此大小,但尝试一下,看看它是否更漂亮,以及您的布局有何变化。在Galaxy选项卡中,图标之间没有间距,但是在nexus中,有一个水平的空间进来了……@theJava我不知道为什么:他们都是JB吗?@JaAd,正是我想要的!
android:layout_width="320dip"