2012-05-03 25 views
-3

并不总是这样的布局:安卓:layout_gravity预期

<?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的水平居中,但为什么没有进度条垂直居中?

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

+0

因为它有'的android:layout_width =“FILL_PARENT”'尝试给它不同的值 – thepoosh

+0

使用相对布局.... – MAC

+0

@Thepoosh使其宽度设置为FILL_PARENT但高度是这里至关重要。 – tomi

回答

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" /> 

尝试上述一个

+0

,这将无法正常工作。 – tomi

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

我发布这个答案之前,我已测试它。它会正常工作。 立即检查... –

+0

想要做到线性布局 – tomi

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>