【问题标题】:Unable to align views properly无法正确对齐视图
【发布时间】:2011-09-26 02:02:26
【问题描述】:

我一直在尝试成功对齐视图,以便它们在其他手机上看起来相同,但我无法成功。我只是无法让它工作。这是背景:

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

措施:
屏幕的左侧和右侧(黄色)是空的.. 40 px..
绿色区域的中心有一个 TextView .. 236 px
橙色区域的中心有一个 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" />

【问题讨论】:

  • 告诉我们每个条带的宽度,并发布您试图用来定位视图的布局。

标签: android layout


【解决方案1】:

如果您希望您的布局灵活适应不同的屏幕尺寸,那么您不想硬编码像素宽度,而是应该使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多