支持不同的屏幕大小 - xml无法正确加载。可能的错误?

时间:2014-01-12 21:11:49

标签: android xml eclipse android-layout layout

最后的问题

我有这个文件夹和文件(省略不重要的文件):

-layout
----activity_map.xml
----list_item_place_map.xml
layout-sw600dp-land
----activity_map.xml
values
----dimens.xml
values-sw600dp
----dimens.xml
values-sw720dp-land
----dimens.xml

布局/ activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapActivity" />

布局/ list_item_place_map.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="48dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin" >

    <TextView
        android:id="@+id/place_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:duplicateParentState="true"
        android:fontFamily="sans-serif-light"
        android:text="@android:string/unknownName"
        android:textColor="@color/missions_item_text"
        android:textSize="18sp" />

</RelativeLayout>

布局sw600dp-脊/ activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false" 
    tools:context=".MapActivity" >

    <ListView
        android:id="@+id/map_places_list"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:divider="@color/missions_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/missions_listselector"
        tools:ignore="InconsistentLayout"
        tools:listitem="@layout/list_item_place_map" />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

值/ dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="main_sign_in_textsize">14sp</dimen>
    <dimen name="main_greeting_textsize">18sp</dimen>
    <dimen name="main_missions_textsize">18sp</dimen>
    <dimen name="main_progress_textsize">40sp</dimen>

</resources>

值-sw600dp / dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <dimen name="main_greeting_textsize">20sp</dimen>

</resources>

值-sw720dp-脊/ dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <dimen name="activity_horizontal_margin">128dp</dimen>

</resources>

显示activity_map.xml时会出现问题。

在肖像上使用应用程序时,它按预期工作(任何屏幕大小) 在横向上使用应用程序时,应该会发生这种情况:

  • 如果低于sw600dp,它应该只显示一张地图(这种情况我没有问题)
  • 如果满足sw600dp的情况,那么它也应该在左侧显示一个列表。 (部分有效)

使用Eclipse Layout Preview,它可以正常运行Nexus 7.但是,在Nexus 10和10.1“预览中,它不显示列表预览(项目未显示,列表显示但是空白)。

我认为它可能只是ADT插件上的一个错误所以我让我的朋友在他的设备上测试APK(Galaxy Tab 2 10.1 with Android 4.2.2)。他给我发了这个截图(私信删除):

ActivityMap preview on Galaxy Tab 2 10.1

如您所见,列表就在那里。元素正确加载。触摸事件没有任何问题。但是,list_item_place_map.xml似乎没有正确加载。

我的猜测是values-sw720dp-land文件夹与layout/list_item_place_map.xml引用activity_horizontal_margin引用布局的方式发生冲突,values/dimens.xml位于values-sw720dp-land/dimens.xml-layout (Blue version) ----activity_map.xml ----list_item_place_map.xml layout-sw600dp-land (Orange version) ----activity_map.xml layout-sw720dp-land (Green version) ----activity_map.xml 。一旦删除引用,Eclipse Layout Peview就会按预期开始工作所有屏幕尺寸。

我的问题是

  • 这是一个错误吗?
  • 如果有,是否已经确认和/或修复?从哪个版本修复?
  • 如何创建变通方法?我不想在不同的文件夹上创建相同文件的副本。我宁愿使用Layout Aliases

修改

我创建了一个简单的项目来展示/展示我的问题(LINK)。

结构如下:

activity_map

place_name在所有3个文件夹中都是相同的,除了右边的颜色。

Test Project PReview on Eclipse

正如您在屏幕截图中看到的(摘自Eclipse Layout Preview),列表未正确显示项目:

  • 10.1 in(使用sw600dp版本)
  • Nexus 10(它使用sw720dp版本)

我在模拟器上测试了项目并得到了相同的结果。

编辑2

我上传了一个小项目,它在github上重现了我的问题:LINK

根据@ anddev84的建议,我使用了HierarchyViewer。到目前为止我发现的是列表中的所有{{1}} TextView都正确加载。但是,RelativeLayout容器无法正确显示。我仍然没有想到它是否在运行时的RelativeLayout或TextView的属性中存在问题,或者是否还有其他事情发生冲突(可能是错误?)。

3 个答案:

答案 0 :(得分:2)

您的ListView的宽度为200 dips,但是您已经在屏幕宽度为720 dips或更高的设备中定义了左/右边距,因为它的项目为128 dips,这样就没有空间了实际内容。您需要更改sw720dp-land的列表项维度,或者将宽度增加到超过256 dips(填充当前占用的空间),或者将填充减少到小于100 dips(这将需要在整个地区)。

答案 1 :(得分:0)

我看到一个错字(也许)?这些应该是一样的吗?

 tools:listitem="@layout/list_item_place_map"

但文件名是:

list_item_places_map.xml

“place_map”! - “places_map”

修改

为了回应您使用错误的文件夹编辑Eclipse,我发现了与值和drawables相同的东西。有时候Eclipse和AVD会使用不同的文件夹,但都会出错(就像使用Galaxy S4一样)。其他时候,至少AVD是正确的。

我最近问过两个问题。其中一个给了我风滚草徽章!

Drawable-xxxx : Eclipse Versus AVD

Android Galaxy S4 uses values-large?

我也看到Eclipse(设置为显示AVD)和某些设备和API的AVD如何呈现布局有很大差异。使用我们提供的开发工具似乎只是一个垃圾射击,只有通过在真实设备上进行测试,您才能真正验证内容的外观。

答案 2 :(得分:0)

我认为问题在于您没有为相关设备的资源目录树指定“完全匹配”。这些都是从开发页面this link中拼写出来的。

根据开发页面,设备最好选择最接近屏幕密度的资源。在原始问题(不是颜色示例)中,您提供'layout-sw600dp-land但没有values-sw600dp-land,因此设备会找到与您提供的横向和其他限定符匹配的第一组值,以及最近的屏幕密度

尝试为所有配置添加values个文件夹(所有需要的屏幕密度,纵向和land),看看是否能为您解决此问题。