【问题标题】:Making the gridView center in Android在Android中制作gridView中心
【发布时间】:2013-03-22 12:30:41
【问题描述】:
<?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>

【问题讨论】:

  • 你试过了吗-> android:layout_gravity="center"
  • 是的,我试过@Santhosh
  • 完全没问题...如果它仍然不起作用,请尝试从您的代码中删除它android:orientation="horizontal"
  • 或者如果你愿意,你可以将你的父布局设置为线性布局

标签: java android


【解决方案1】:

好吧,如果您不想使用 TableLayout (as you says in your precedent post),那么处理 GridView 是相当复杂的(我在这方面迷失了很多次)。 我唯一的想法是强制您的 GridView 具有精确的宽度,即您的 2 个视图的总和:160dip * 2。

    android:layout_width="320dip"

随意添加 10dip 或更多的填充。

注意:我不确定这是否是最好的方法,以及这是否适用于所有设备。试试看,让我知道;)。

【讨论】:

  • 快速提问:我的图片大小是 150dp,列宽是否也应该是 150dp 或更多
  • @theJava 你可以保留这个尺寸,但试试看它是否更漂亮,你的布局有什么变化。
  • 在 Galaxy 选项卡中,图标之间没有间距,但在 nexus 中有一个水平空间进入....
  • @theJava 真诚地我不知道为什么:他们都和 JB 在一起吗?
  • @JaAd,正是我想要的!
【解决方案2】:

在你的gridview中使用这2个标签看看它是否有效

android:layout_centerVertical="true"
android:layout_centerHorizontal="true"

【讨论】:

  • 你能在 Gridview 和 relativelayout 中删除重力标签和 centerInParent 标签,并且只在 gridview 中使用这两个标签并查看...
  • 我也试过上面的,没有任何改变...pastie.org/private/zfinfrxvxmwirw0yvr5qg
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多