2011-08-11 26 views
0

這裏是XML:centerHorizo​​ntal不居中正確的圖像

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:background="@drawable/bg" 
     > 
    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="4" 
      android:orientation="vertical" 
      > 
     <RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:gravity="center_horizontal" //CENTER 01 
       > 
      <!--center button--> 
      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/img" 
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true" //CENTER 02 
        /> 
     //...    
     </RelativeLayout>   
    </LinearLayout> 
    <include android:id="@+id/banner" layout="@layout/banner" 
      android:layout_weight="1" 
      android:layout_gravity="bottom"/> 
</LinearLayout> 

中間按鍵爲中心,而不是在屏幕上真正的中心,而是約50像素的中心的右側。如果我刪除了centerHorizontal CENTER01,那麼這不會發生。

有誰知道爲什麼會發生這種情況?

+0

等等,所以當CENTER01被刪除時它工作正常嗎? –

回答

1

此問題可能是由於使用的XML文件中既相對佈局及線性佈局。

改變所有線性佈局相對佈局使圖像如你預期查看將居中。

我也在單個XML中混合了線性佈局和相對佈局,並面臨這種問題。一旦我將所有線性佈局替換爲相對佈局,我的問題就解決了。

嘗試it.Thanks.Venky

3

請嘗試爲//CENTER 02

android:layout_centerInParent="true" 
1

當您在父設置android:gravity="center_horizontal",它影響了孩子們的行爲。因此,你的ImageView受到影響,產生你在被其他屬性放置在中心後看到的效果。

您可以通過將TextView放在那裏與android:layout_width="fill_parent"一起看到這個效果。

1

因爲在相對佈局中center_horizo​​ntal不是命令。適用於以線性佈局封裝的視圖。爲RelativeLayout的需要使用

android:layout_centerInParent="true";

爲中心重力命令線性

android:layout_centerHorizontal="true" 

如線性重力CENTER_HORIZONTAL。