Android: Center view content doesn't center vertically
I am trying to get a ProgressBar and TextView to be centered within a ViewSwitcher. They can be positioned vertically above each other although I have no problem if they were centered horizontally next to each other. I am able to get them to center horizontally but not vertically开发者_开发知识库. It seems that the height of the outer view (the ViewSwitcher) doesn't fill vertically although every parent view has android:layout_height="fill_parent"
Here is the 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>
Set your LinearLayout
's height to be wrap_content
and it should also center it vertically. Also you should add android:layout_gravity="center"
on the LinearLayout
to center it inside the ViewSwitcher
Use the code below:
<?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>
精彩评论