无法正确对齐视图

时间:2011-09-25 17:53:06

标签: android layout

我一直试图成功调整视图,使其在其他手机上看起来一样,但我无法成功。我无法让它发挥作用。这是背景:

enter image description here

我希望TextView位于绿色区域的中间,蓝色区域的中间,以及橙色区域中的imageview。我已经问过这个了,我得到了一个使用layout_weight here的建议。但我无法正确计算重量。我怎样才能做到这一点? layout_weight是正确的方法吗?我该如何计算呢?

措施:
屏幕的左侧和右侧(黄色)是空的..每个40 px ..
绿色区域在中心有一个TextView .. 236 px
橙色区域在中心有一个图像视图.. 44像素
蓝色区域的中心有一个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 :(得分: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

只需将这些像素值转换为宽度值,并将这些像素值的总和用作父级中的weightSum

相关问题