2011-09-25 94 views
0

我一直在嘗試成功對齊視圖,以便它們在其他手機上看起來相同,但我無法成功。我只是無法讓它工作。這裏的背景是:無法正確對齊視圖

enter image description here

我想在綠燈區中部一個TextView,而在藍色區域的中部,並在橙色區域的ImageView的。我已經問過這個,我有一個建議使用layout_weight here。但我無法正確計算重量。我怎樣才能做到這一點? layout_weight是否是正確的路線?我如何計算它?

措施:
屏幕的左側和右側(黃色)爲空.. 40像素每..
綠區具有在centrer一個TextView .. 236像素
橙色區域具有一個imageview的在中心.. 44 PX
藍色區具有在中心處一個TextView .. 120 PX

我用於custom_row的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:orientation="horizontal" 
android:layout_height="wrap_content" android:paddingTop="10dip" android:paddingBottom="10dip"> 

<TextView android:id="@+id/Start_Numbering" android:textSize="19.5dip" 
    android:layout_width="0dp" android:layout_height="wrap_content" 
    android:layout_weight="0.3" android:background="@drawable/list_number_bg" 
    android:gravity="center" 
    /> 

<ImageView android:id="@+id/Start_ImageView" 
    android:layout_weight="0.1" android:layout_height="fill_parent" android:scaleType="center" 
    android:layout_width="0dp" android:src="@drawable/list_noaudioavailable" 
    android:gravity="center" 
    ></ImageView> 


<TextView android:id="@+id/Start_Name" android:textColor="#a7e9fe" 
    android:textSize="25dip" android:layout_width="0dp" 
    android:layout_weight="0.6" 
    android:gravity="center" android:background="@drawable/list_name_bg" 
    android:layout_height="wrap_content" /> 

+1

告訴我們每個帶的寬度,併發布您嘗試用來定位視圖的佈局。 –

回答

0

如果您希望您的佈局對於不同的屏幕尺寸具有靈活性,那麼您不需要硬編碼像素寬度,而應該像使用上一個問題中的答案那樣使用layout_weights。對於ViewGroup,您可以定義總計weightSum,然後必須爲每個必須加起來爲母公司的weightSum的子項定義單個權重。下面是一個簡單的例子,類似於你在上面用黑色和白色兩種顏色描述了:

<LinearLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="20"> 
     <View android:id="@+id/view1" 
      android:layout_weight="2" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:background="@android:color/white"/> 

     <View android:id="@+id/view2" 
      android:layout_weight="7" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:background="@android:color/black"/> 

     <View android:id="@+id/view3" 
      android:layout_weight="2" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:background="@android:color/white"/> 

     <View android:id="@+id/view4" 
      android:layout_weight="7" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:background="@android:color/black"/> 

     <View android:id="@+id/view5" 
      android:layout_weight="2" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:background="@android:color/white"/> 

</LinearLayout> 

The measures: 
The left and right side of the screen (yellow) are empty.. 40 px each.. 
The green zone have a TextView at the centrer .. 236 px 
The orange zone has an imageview at the center .. 44 px 
The blue zone has a TextView at the center .. 120 px 

只是轉換的像素VALES成寬valuse並使用這些像素值作爲weightSum的總和父