Android 如何对齐Gridview中心

Android 如何对齐Gridview中心,android,gridview,Android,Gridview,我想在中心对齐GridView 我还是这样: <GridView android:id="@+id/gridView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="55sp" android:background="#ffff" android:clickable="true" andr

我想在中心对齐GridView

我还是这样:

<GridView
    android:id="@+id/gridView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="55sp"
    android:background="#ffff"
    android:clickable="true"
    android:columnWidth="100dp"
    android:drawSelectorOnTop="true"
    android:focusable="true"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp"/>

正如你们在上图中所看到的,我在左边得到了图像,但我想在中间显示图像,如下图所示:

另一件事是获得额外的文本背景宽度我怎么能只在图像中显示文本背景

现在gridview处于中心位置,但仍然存在问题,请参见文本背景巨宽,这将超出imageview:

请检查更新的xml脚本

活动\u main.xml:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/header_bg"
        android:gravity="center"
        android:text="@string/header_main"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:textStyle="bold" />



     <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal"
        >

    <GridView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:columnWidth="@dimen/photo_size"
        android:horizontalSpacing="@dimen/photo_spacing"
        android:numColumns="4"
        android:gravity="center"
        android:padding="4dp"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:layout_gravity="center"
        android:verticalSpacing="@dimen/photo_spacing" />

</LinearLayout>

</RelativeLayout>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/album_item"
   android:layout_width="match_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

<ImageView
    android:id="@+id/cover"        
    android:contentDescription="@string/app_name"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="center" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:layout_alignBottom="@+id/cover"
        android:background="#70000000"
        android:gravity="center"
        android:padding="6dp"
        android:textColor="@color/white"
        android:textSize="12sp"
        android:textStyle="bold" />

</RelativeLayout>

row.xml:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/header_bg"
        android:gravity="center"
        android:text="@string/header_main"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:textStyle="bold" />



     <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal"
        >

    <GridView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:columnWidth="@dimen/photo_size"
        android:horizontalSpacing="@dimen/photo_spacing"
        android:numColumns="4"
        android:gravity="center"
        android:padding="4dp"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:layout_gravity="center"
        android:verticalSpacing="@dimen/photo_spacing" />

</LinearLayout>

</RelativeLayout>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/album_item"
   android:layout_width="match_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

<ImageView
    android:id="@+id/cover"        
    android:contentDescription="@string/app_name"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="center" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:layout_alignBottom="@+id/cover"
        android:background="#70000000"
        android:gravity="center"
        android:padding="6dp"
        android:textColor="@color/white"
        android:textSize="12sp"
        android:textStyle="bold" />

</RelativeLayout>

如果我使用TextView作为
wrap\u内容
,那么如下所示:

<GridView
    android:id="@+id/gridView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="55sp"
    android:background="#ffff"
    android:clickable="true"
    android:columnWidth="100dp"
    android:drawSelectorOnTop="true"
    android:focusable="true"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp"/>

尝试
android:gravity=“center”
查看包含
文本视图的
线性布局
,以对齐中心的文本:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/cover"
android:gravity="center"
android:background="#70000000"
android:padding="6dp" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:gravity="center"
    android:singleLine="true"
    android:textColor="@color/white"
    android:textSize="12sp"
    android:textStyle="bold" />

</LinearLayout>

要使Gridview与中心对齐,请使用:

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_below="@+id/textView2"
android:orientation="horizontal"
>

<GridView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:columnWidth="@dimen/photo_size"
android:horizontalSpacing="@dimen/photo_spacing"
android:numColumns="auto_fit"
android:layout_gravity="center"
android:padding="4dp"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="@dimen/photo_spacing" />

</LinearLayout>



这将解决您的问题。

您只需要使用android:layout\u gravity=“center”
作为其线性布局的子对象

首先,将父LinearLayout的宽度更改为
match\u parent
。 由于现在它的
包装内容
,在中间对齐将没有用

要将栅格视图居中对齐,请执行以下操作:

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/textView2"
    android:orientation="horizontal"
    >

    <GridView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:columnWidth="@dimen/photo_size"
        android:horizontalSpacing="@dimen/photo_spacing"
        android:numColumns="auto_fit"
        android:padding="4dp"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:layout_gravity="center"
        android:verticalSpacing="@dimen/photo_spacing" />

</LinearLayout>
我使用以下方法在图像中央显示标题:


您只需添加
android:layout\u centerInParent=“true”
并删除
gravity
。希望这有帮助。

使用此代码将图像设置在中心:

 <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:scaleType="centerCrop"/>

这是一个很好的编码方式…

试试
android:gravity=“center”
查看包含
TextView
LinearLayout,谢谢我在LinearLayout中使用了android:gravity=“center”,但主要的挑战是如何对齐gridview中心?它解决了你的问题吗?尝试
android:gravity=“center”
查看你的
GridView
你是否尝试了
android:layout\u gravity=“center”
而不是
android:gravity=“center”
查看行中的文本。xml?尝试
gravity=“center”
你的
活动,尝试使用android:layout\u gravity=“center”
来获得gridview解决方案,你会大吃一惊。我知道,但不想在线性布局中使用它。好把戏!