android:layout_gravity并不总是按预期工作

时间:2012-05-03 07:36:32

标签: android

这种布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >

<TextView
    android:id="@+id/main_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:keepScreenOn="true"
    android:text="@string/hello"
    android:textAppearance="@android:style/TextAppearance.Large"
    android:textColor="#0000ff" />

<ProgressBar
    android:id="@+id/main_progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical" />

</LinearLayout>

textView是水平居中的,但为什么ProgressBar没有垂直居中?

PS。我知道我可以使用相对布局来做到这一点。但我不明白为什么它不能使用Linear? PS2。为什么选票下降,这是一个完全合法的问题?

3 个答案:

答案 0 :(得分:0)

<ProgressBar
    android:id="@+id/main_progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical" />

尝试上面的那个

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >

<TextView
    android:id="@+id/main_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:keepScreenOn="true"
    android:text="@string/hello"
    android:textAppearance="@android:style/TextAppearance.Large"
    android:textColor="#0000ff" />

<ProgressBar
    android:id="@+id/main_progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true" />

</RelativeLayout>

答案 2 :(得分:0)

首先,我意识到这是一个非常古老的问题,但也许有人在寻找这个可能会发现这有用。

我不完全确定为什么你的确切例子不起作用,如果你现在这样做,请务必告诉我。我知道如果你为进度条添加额外的LinearLayout它会起作用。 像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/main_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:keepScreenOn="true"
        android:text="@string/hello"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:textColor="#0000ff" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ProgressBar
            android:id="@+id/main_progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" />
    </LinearLayout>

</LinearLayout>
相关问题