几天来我一直在尝试通过使用多层嵌套进行转换来提高布局效率LinearLayouts
to one 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) The android:layout_*
在 include 标记中使用属性时似乎会被忽略,并且所有合并的布局都显示在彼此的顶部。根据这篇文章(http://developer.android.com/resources/articles/layout-tricks-reuse.html http://developer.android.com/resources/articles/layout-tricks-reuse.html) "any android:layout_*
属性可以与<include />
tag"
2)由于我无法让这个工作,我决定尝试添加一个android:layout_below
归因于第一个TextView
item 在每个合并布局文件中,这意味着每个合并文件将引用另一个合并布局文件中的 id...在大多数情况下,这实际上有效,并且我的布局看起来很好。但是,我在其中之一上收到错误android:layout_below
属性说它找不到我指定的 id...我已经对 id 进行了两次和三次检查以确保它们是正确的。最奇怪的部分是我使用了AutoFill
功能是将 id 放在属性的第一位。
如果有人有任何建议或解决方法,我将非常乐意尝试。另外,如果有人能想出一种方法让我只拥有一个合并 xml 布局文件而不是 5 个,我将不胜感激。我找不到方法来做到这一点,因为我需要在运行时访问合并布局文件中的每个项目......