【问题标题】:Trouble - Same Layout in Different Densities麻烦 - 不同密度的相同布局
【发布时间】:2012-02-28 22:18:08
【问题描述】:

我在 HVGA 和 WVGA800 分辨率下测试我的应用程序都是普通屏幕,所以我创建了中等和高密度的图像,但我不明白为什么我在 HVGA 中的文本“Orientação”(标题)与 WVGA800 相比是分散的,我用“dp”单位编码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView
        android:id="@+id/imageCanadaNavBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:src="@drawable/canada_navbar"
        android:onClick="imgTop_Click"  />       
   <ImageView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/imageCanadaNavBar"
        android:src="@drawable/bg_viagem"
        android:scaleType="fitXY" />                                
    <TextView 
        android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"            
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_marginTop="30dp"             
        android:textColor="#000000"
        android:textSize="19sp"
        android:gravity="center|center_vertical"        
        android:layout_below="@id/imageCanadaNavBar" />    
    <ScrollView 
        android:id="@+id/scrollTextoDetalhe"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="7dp" 
        android:layout_below="@id/txtTitle">                       
        <TextView 
            android:id="@+id/txtDetalhe"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"                       
            android:textColor="#FFFFFF"
            android:textSize="15dp"
            android:paddingRight="25dp"
            android:paddingLeft="25dp"
            android:paddingBottom="17dp"
            android:paddingTop="17dp" />        
    </ScrollView>
</RelativeLayout>

【问题讨论】:

  • 能否也贴一下周边的布局代码? (父视图和兄弟视图)
  • @kabuko 是的,当然,我在上面添加了完整的 xml 布局。

标签: java android android-layout android-emulator


【解决方案1】:

尝试使用sp (Scale-independent Pixels) 而不是dp (Dentisy-Independent Pixels)

建议将其用于文本大小单位。

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

【讨论】:

  • 我试过用 sp 代替 dp 但效果是一样的。我要疯了!
【解决方案2】:

我建议不要为 TextView 的背景使用单独的 ImageView,而是将其设置为背景。您还应该确保 Drawable 是一个 9-patch,以便它可以正确缩放并调整您的填充/边距。所以而不是:

<ImageView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/imageCanadaNavBar"
    android:src="@drawable/bg_viagem"
    android:scaleType="fitXY" />                                
<TextView 
    android:id="@+id/txtTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"            
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:layout_marginTop="30dp"             
    android:textColor="#000000"
    android:textSize="19sp"
    android:gravity="center|center_vertical"        
    android:layout_below="@id/imageCanadaNavBar" />

你应该有类似的东西:

<TextView 
    android:id="@+id/txtTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"            
    android:padding="10dp"
    android:textColor="#000000"
    android:textSize="19sp"
    android:gravity="center|center_vertical"       
    android:background="@drawable/bg_viagem" 
    android:layout_below="@id/imageCanadaNavBar" />

【讨论】:

    【解决方案3】:

    在我的例子中,我将所有图像分开以在 XML 中制作一个独特的图像。

    这是我的最终应用 Canada Destino

    【讨论】:

      【解决方案4】:

      您的边距和内边距设置使其在一个实例中看起来居中,尽管实际上并非如此。

      android:gravity="center" 更改为android:gravity="center|center_vertical",您的问题应该会得到解决(您可能需要稍微调整一下边距和填充)。

      【讨论】:

      • 我测试了你的提示,但 HVGA 设备上的布局仍然错误。
      • 您是否删除了上边距?我相信居中是在边距之后完成的,因此拥有上边距但没有下边距会使所有内容都向下移动。不在家里,所以我无法仔细检查,但我相信是这样的。
      猜你喜欢
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      相关资源
      最近更新 更多