android - 如何对齐Gridview中心

标签 android gridview

我想将 GridView 居中对齐

我还是这样:

enter image description here

如您在上图中所见,我在左侧获取图像,但我想在中心显示图像,如下所示:

enter image description here

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

现在 gridview 位于中心,但仍然有问题,看到文本背景宽度超出 imageview:

enter image description here

请检查更新的 xml 脚本

activity_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>

row.xml:-

 <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_content 然后会变成这样:

enter image description here

最佳答案

你只需要使用 android:layout_gravity="center" 作为它的 LinearLayout 的 child 。

首先,将父 LinearLayout 的宽度更改为 match_parent。 由于它的 wrap_content 现在居中对齐是没有用的

将 GridView 居中对齐:

<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>

以及居中对齐文本:

试试这个:

<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:singleLine="true"
        android:textColor="@color/white"
        android:textSize="12sp"
        android:layout_gravity="center"
        android:textStyle="bold" />

</LinearLayout>

希望对您有所帮助。

更新:

要将标题居中对齐,只需修改重力即可。由于 row.xml 中的父布局是 RelativeLayout,因此 layout_gravity 不会产生任何影响。使用 centerInParent

例子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/album_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:background="#eeeeee"
        android:id="@+id/cover"
        android:src="@drawable/ic_launcher"
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:contentDescription="@string/app_name"
        android:scaleType="center" />

    <TextView
        android:id="@+id/title"
        android:text="@string/iclauncher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/cover"
        android:background="#70000000"
        android:ellipsize="end"
        android:layout_centerInParent="true"
        android:padding="6dp"
        android:singleLine="true"
        android:textSize="12sp"
        android:textStyle="bold" />

</RelativeLayout>

我使用这个在图像中心获取标题:

enter image description here

您只需添加 android:layout_centerInParent="true" 并移除 gravity。希望这会有所帮助。

关于android - 如何对齐Gridview中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26118349/

相关文章:

android - googlesigninbutton 对点击没有反应

android - 在游标 android 中写入

android - 如何在我的图像中制作发光效果?

c# - 帮助 C# DevExpress XtraGrid GridControl - 使单元格中的复选框不可见

java - 运行时添加 Gridview 中的新项目

安卓。如何从我的应用程序中读取 Aztec 代码?

android - 如何将填充分配给 Listview 项目分隔线

android - PhoneGap 应用程序中的初始屏幕尺寸

javascript - 如何在 javascript 中计算 gridview 中的行数?

php - yII2:日期过滤器如何在 Gridview 中