2014-09-30 2 views
1

Я хочу, чтобы выровнять 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

+1

Попробуйте 'android: gravity =" center "' для 'LinearLayout', который включает в себя' TextView' –

+0

спасибо, что я использовал android: gravity = "center" в LinearLayout, но главная задача - выровнять центр сетки? – Sun

+0

он решил ваш вопрос? попробуйте 'android: gravity =" center "' для вашего 'GridView' –

ответ

2

Вам нужно всего лишь использовать android:layout_gravity="center" в качестве своего ребенка LinearLayout.

И, во-первых, измените ширину родительской 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> 

Надеюсь, что это поможет.

UPDATE:

Для выравнивания надписи в центре, просто измените гравитацию. Поскольку ваш родительский макет в 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. Надеюсь это поможет.

+0

проверьте мой обновленный скрипт вместе с снимками экрана. – Sun

+0

Для решения сетки вы качаете. Я знал, но не хотел использовать его в линейной компоновке. Хороший трюк! – sud007

0

Попробуйте android:gravity="center" для LinearLayout, который окружает TextView для выравнивания текст в центре:

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

не сделано Брат все еще выравнивает сетку влево – Sun

+0

Попробуйте 'gravity =" center "' your 'activity_main.xml:' 'RelativeLayout' –

+0

попробовал не делать .. – Sun

2
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_gravity="center" 
android:orientation="vertical" > 

Это позволит решить вашу проблему.

Смежные вопросы