android - 支持不同的屏幕尺寸 - xml 未正确加载。可能的错误?

标签 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

layout/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" />

layout/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>

layout-sw600dp-land/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>

values/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>

values-sw600dp/dimens.xml:

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

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

</resources>

values-sw720dp-land/dimens.xml:

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

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

</resources>

显示 activity_map.xml 时出现问题。

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

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

使用 Eclipse Layout Preview,它在 Nexus 7 上按预期工作。但是,在 Nexus 10 和 10.1"预览中,它不显示列表预览(项目未显示,列表出现但为空)。

我认为这可能只是 ADT 插件上的错误,所以我让我的 friend 在他的设备(Galaxy Tab 2 10.1 和 Android 4.2.2)上测试了 APK。他给我发了这张截图(删除了私有(private)信息):

ActivityMap preview on Galaxy Tab 2 10.1

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

我的猜测是文件夹 values-sw720dp-land 与布局的加载方式发生冲突,因为 layout/list_item_place_map.xml 引用了 activity_horizo​​ntal_margin,位于 values/dimens.xmlvalues-sw720dp-land/dimens.xml 中。一旦我删除引用,Eclipse Layout Peview 就会开始按预期在所有屏幕尺寸下工作。

我的问题是

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

编辑:

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

结构如下:

-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

activity_map 在所有 3 个文件夹上都相同,除了右侧的颜色。

Test Project PReview on Eclipse

如您在屏幕截图(取自 Eclipse 布局预览)中所见,列表未正确显示项目:

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

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

编辑 2:

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

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

最佳答案

您的 ListView 的硬宽度为 200 度,但您已将其项目的左/右填充定义为在屏幕宽度为 720 度或更大的设备中为 128 度,这就留下了没有实际内容的空间。您将需要更改 sw720dp-land 的列表项尺寸,将宽度增加到超过 256 个凹陷(当前填充占用的空间),或者将填充减小到小于 100下降(这将占据整个区域)。

关于android - 支持不同的屏幕尺寸 - xml 未正确加载。可能的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21080338/

相关文章:

android - AppAutoStartupPolicy : prevent start service of package com. farsitel.bazaar

php - 从服务器检索数据花费了太多时间

android - fragment 或 Activity 中带有工具栏的协调器布局

java - 导入 Web 项目时出现“无法解析为类型”错误

android - 具有两列和自动调整大小的图像的 Gridview

xml - Delphi 5 中的 Web 服务

PHP 和 XML (PT) 时间格式

java - Spring注释与hibernate集成中的文件未找到异常

java - STS - Spring Boot 应用程序 - 请求的资源不可用

java - Eclipse 中的循环