安卓:视图内容居中显示时垂直方向未居中

3
我正在尝试让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

将你的LinearLayout的高度设置为wrap_content,并使其在垂直方向上居中。此外,您还应该在LinearLayout上添加android:layout_gravity="center"以使其在ViewSwitcher内居中。


它不起作用。我甚至尝试在ViewSwitcher上使用wrap_content,但它们始终保持在视图顶部居中。 - Johann
哎呀,忘了提醒您还需要在LinearLayout上设置android:layout_gravity="center"。我会更新我的答案。 - Jason Robinson

1
请使用以下代码:
<?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>

使用以下解决方案解决问题: https://dev59.com/FnI-5IYBdhLWcg3wO1vl - Johann
当然。RelativeLayout也可以通过适当的设置实现。我假设您想要一个LinearLayout的解决方案。 - Mandel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接