如何对齐Gridview中心 [英] How to align Gridview center
问题描述
我想在中心对齐GridView
I want to align GridView in center
我还是这样:
如上图所示,我正在左侧获取图像,但是我想在中央显示图像,如下所示:
As you can see in above image, i am getting images on left hand side, but i want to show images in center like below:
另一件事是增加文本背景的宽度我如何仅在图像中显示文本背景
another thing getting extra width of text background How can i show text background within image only
现在gridview位于中心,但仍然出现问题,请查看文本背景的宽宽度超出了imageview的范围:
now gridview is in center, but still getting issue, see text background huge width which is going out of imageview:
请检查更新的xml脚本
Please check updated xml script
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
,则如下所示:
and if i use TextView as wrap_content
then getting like this:
推荐答案
您只需使用android:layout_gravity="center"
作为其LinearLayout的子级.
You would just need to use android:layout_gravity="center"
as its LinearLayout's child.
首先,将父级LinearLayout的宽度更改为match_parent
.
就像现在的wrap_content
一样,在中心对齐将毫无用处
And firstly, change parent LinearLayout's width to match_parent
.
As its wrap_content
right now, it will be of no use aligning in center
要在中心对齐GridView:
To align the GridView in center:
<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
.
For aligning the caption in center, just modify your gravity. As your parent layout in row.xml is RelativeLayout, layout_gravity won't make any effect. Use 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>
我正在使用此标题显示在图像中心:
I am getting the caption in centre of image using this:
您只需要添加android:layout_centerInParent="true"
并删除gravity
.希望这会有所帮助.
You will just have to add android:layout_centerInParent="true"
and remove gravity
. Hope this helps.
这篇关于如何对齐Gridview中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!