2011-04-12 66 views
0

我有一個問題,我的imagebutton它沒有對齊時,我指定了centerinparent。Imagebutton layout_center_inparent問題

<LinearLayout 
     android:layout_width="match_parent" android:layout_marginTop="4dp" 
     android:layout_marginLeft="4dp" android:layout_marginRight="4dp" 
     android:layout_height="50dp" android:background="#FF0000"> 

     <RelativeLayout android:layout_height="match_parent" 
      android:layout_width="40dp" android:background="#0000FF"> 
      <ImageView android:id="@+id/imgType" 
       android:layout_centerInParent="true" android:layout_width="wrap_content" 
       android:src="@drawable/type_text" android:layout_height="wrap_content" /> 
     </RelativeLayout> 

     <RelativeLayout android:layout_height="match_parent" 
      android:layout_width="match_parent" android:layout_weight="1"> 
      <LinearLayout android:orientation="vertical" 
       android:layout_height="match_parent" android:layout_width="match_parent"> 
       <TextView android:layout_width="match_parent" 
        android:textColor="#686a68" android:layout_height="match_parent" 
        android:textSize="15dp" android:text="Manifest : Text" 
        android:ellipsize="end" android:singleLine="true" 
        android:layout_weight="1" /> 

       <TextView android:layout_width="match_parent" 
        android:textColor="#686a68" android:layout_height="match_parent" 
        android:textSize="15dp" android:id="@+id/txtTitle" android:text="[TEXT NAME]" 
        android:layout_weight="1" android:singleLine="true" 
        android:ellipsize="end" /> 
      </LinearLayout> 
     </RelativeLayout> 

     <RelativeLayout android:layout_height="match_parent" 
      android:layout_width="60dp" android:background="#00FF00"> 
      <ImageButton android:src="@drawable/type_text" 
       android:layout_height="48dp" android:layout_centerInParent="true" 
       android:layout_width="48dp" android:id="@+id/butEdit" /> 
     </RelativeLayout> 

    </LinearLayout> 

image

正如你所看到的ImageButton的垂直對齊方式是不正確的。

它爲什麼這樣做。

+1

你試過'android:gravity =「center」'而不是? – rajath 2011-04-12 09:33:37

+0

嗨。葉普做同樣的事情。 – Pintac 2011-04-12 09:44:02

回答

0

您是否已嘗試在模擬器上運行此操作並使用hierarchyviewer工具(位於SDK的「tools」文件夾中,單擊列表中的應用程序並選擇「Load View Hierarchy」)來檢查有問題的元素?看看它的容器問題控制&的邊距和填充。還要看各種組件的寬度,它可能會讓您知道問題發生的位置。

同時檢查你的drawable在@ drawable/type_text中是否包含任何可能導致感知偏移量的空間(我認爲不太可能)。

我偶爾會看到縮放圖像的奇怪問題,所以如果@ drawable/type_text不是48x48像素,請嘗試在Photoshop/GIMP/PSP中手動將圖像縮放到所需的大小,並查看是否有所作爲。

或者嘗試使用重力嘗試中心您控制:

<LinearLayout android:layout_height="match_parent" android:gravity="center" 
    android:layout_width="60dp" android:background="#00FF00"> 
    <ImageButton android:src="@drawable/type_text" 
     android:layout_height="48dp" 
     android:layout_width="48dp" android:id="@+id/butEdit" /> 
</LinearLayout> 

或許:

<LinearLayout android:layout_height="match_parent" android:gravity="center" 
    android:layout_width="wrap_content" android:background="#00FF00"> 
    <ImageButton android:src="@drawable/type_text" 
     android:layout_height="48dp" android:layout_width="48dp"        
     android:layout_margin="12dp" android:id="@+id/butEdit" /> 
</LinearLayout> 

如果這些方法都失敗,請嘗試調整個人機器人:layout_margin *的的ImageButton的屬性嘗試和強制的東西。

我希望其中的一些幫助。

+0

yip imagebutton中有一些時髦的填充。嘗試兩者,並嘗試設置填充0dp仍然沒有任何 – Pintac 2011-04-12 10:46:07

+0

圖像按鈕中的填充將控制邊框和圖像本身(即在按鈕邊框內)之間的距離。邊距將控制邊界與其父邊之間的距離(即在按鈕邊界之外)。因此請查看邊距設置而不是填充。或者查看作爲ImageButton父級的[Relative | Linear] Layout的填充。 – 2011-04-12 11:41:48

+0

這就是有趣的事情imagebutton沒有保證金只有填充和相對佈局沒有他們 – Pintac 2011-04-12 11:51:27