android - 如何让 RelativeLayout 与合并和包含一起使用?

标签 android android-layout merge include android-relativelayout

我这几天一直在尝试通过从使用多层嵌套 LinearLayouts 转换来提高布局效率。到一个 RelativeLayout并且遇到了一些我无法找到解决方法的问题......

我已经搜索了 Android 初学者组和这个网站,但找不到任何可以帮助我解决问题的东西。

我在其中一个博客上读到,您可以将布局与合并和包含标签结合起来。所以我有一个带有 RelativeLayout 的主布局文件。根元素。其中我有 5 个包含标签,它们引用了 5 个不同的 xml 布局文件,每个文件都有一个用于根的合并元素(我的所有合并文件都是相同的,除了其中的 id)。

我遇到了两个问题,我会在发布我的布局代码的简化版本后解释:

示例主布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/translucent_gray" >

    <include 
        android:id="@+id/running_gallery_layout_id"
        layout="@layout/running_gallery_layout" />

    <include 
        android:id="@+id/recent_gallery_layout_id" 
        layout="@layout/recent_gallery_layout"
        android:layout_below="@id/running_gallery_layout_id" />

    <include
        android:id="@+id/service_gallery_layout_id"
        layout="@layout/service_gallery_layout"
        android:layout_below="@id/recent_gallery_layout_id" />

    <include
        android:id="@+id/process_gallery_layout_id"
        layout="@layout/process_gallery_layout"
        android:layout_below="@id/service_gallery_layout_id" />

</RelativeLayout>

包含的合并文件示例:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView 
        style="@style/TitleText"
        android:id="@+id/service_gallery_title_text_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="@string/service_title" />

    <Gallery
        android:id="@+id/service_gallery_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_below="@id/service_gallery_title_text_id" />

    <TextView 
        style="@style/SubTitleText"
        android:id="@+id/service_gallery_current_text_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/service_gallery_title_text_id"
        android:layout_above="@id/service_gallery_id" />
</merge>

我遇到了两个问题:

1) android:layout_*在包含标签中使用时似乎会忽略属性,并且所有合并的布局都显示在彼此的顶部。根据这篇文章( http://developer.android.com/resources/articles/layout-tricks-reuse.html )“任何 android:layout_* 属性都可以与 <include /> 标签一起使用”

2) 由于无法正常工作,我决定尝试添加 android:layout_below属性到第一个 TextView每个合并布局文件中的项目,这意味着每个合并文件都将引用另一个合并布局文件中的 id ......在大多数情况下,这实际上有效,我的布局看起来很好。但是,我在 android:layout_below 之一上遇到错误。属性说它找不到我指定的 id...我已经两次和三次检查了 id 以确保它们是正确的。最奇怪的部分是我使用了 AutoFill将 id 放在属性中的功能。

如果有人有任何建议或解决方法,我将非常乐意尝试。此外,如果有人能想到一种方法让我只拥有一个合并 xml 布局文件而不是 5 个,那将不胜感激。我找不到这样做的方法,因为我需要在运行时访问合并布局文件中的每个项目...

最佳答案

包含标签存在问题。查询:https://issuetracker.google.com/issues/36908001

要修复它,请确保在包含时同时覆盖 layout_widthlayout_height,否则所有内容都将被忽略。

关于android - 如何让 RelativeLayout 与合并和包含一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2316465/

相关文章:

android - 如何从listView上的选定项目中提取文本

android - 计算器应用程序在相同屏幕尺寸但不同 DPI 上的不同布局(420 DPI 与 480 DPI)

visual-studio - TFS 分支与合并

svn - TortoiseSVN 将文件标记为冲突,但在 TortoiseMerge 中看不到冲突

android - 在 Android Wear 上实现 GridViewPager 的正确方法是什么?

java - 如何更新和增量数据库?

android - 如何以编程方式添加联系人

Android In-App Billing 失败,提示 "You already have a pending order for this item."

android - 2.3 崩溃应用程序中的 fragment

git -- 如何从未完成的 merge 中恢复