Android: центр просмотра контента не центрируется вертикально - PullRequest
2 голосов
/ 14 июля 2011

Я пытаюсь получить ProgressBar и TextView в центре ViewSwitcher. Они могут быть расположены вертикально друг над другом, хотя у меня нет проблем, если они были расположены горизонтально по центру друг от друга. Я могу заставить их центрироваться горизонтально, но не вертикально. Кажется, что высота внешнего вида (ViewSwitcher) не заполняется по вертикали, хотя у каждого родительского вида есть android: layout_height = "fill_parent"

Вот XML:

<ViewSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loadSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:background="#ff000000">
  <ProgressBar
    android:indeterminate="true"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/marker_progress"
    style="?android:attr/progressBarStyle"
    android:gravity="center_vertical" />
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="18sp"
    android:text="Loading data..."
    android:gravity="center" />
</LinearLayout>
<ListView
  android:id="@+id/android:appCategoriesList"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#ff918e8e"
  android:divider="#ff000000"
  android:dividerHeight="1dp"
  android:cacheColorHint="#00000000"
  android:scrollingCache="true"
  android:clickable="false"
  android:focusable="false" />
</ViewSwitcher>

Ответы [ 2 ]

2 голосов
/ 14 июля 2011

Установите высоту LinearLayout равной wrap_content, и она также должна быть центрирована по вертикали. Также вы должны добавить android:layout_gravity="center" на LinearLayout, чтобы центрировать его внутри ViewSwitcher

1 голос
/ 14 июля 2011

Используйте код ниже:

<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/loadSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:background="#ff000000">
        <ProgressBar
            android:indeterminate="true"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/marker_progress"
            style="?android:attr/progressBarStyle"
            android:layout_gravity="center"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="Loading data..." 
            />
    </LinearLayout>
    <ListView
        android:id="@+id/android:appCategoriesList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ff918e8e"
        android:divider="#ff000000"
        android:dividerHeight="1dp"
        android:cacheColorHint="#00000000"
        android:scrollingCache="true"
        android:clickable="false"
        android:focusable="false" />
</ViewSwitcher>
...