2011-09-25 99 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的总和父