如何让RelativeLayout 与合并和包含一起工作? [英] How to get RelativeLayout working with merge and include?

查看:25
本文介绍了如何让RelativeLayout 与合并和包含一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我搜索了 Android 初学者组和这个网站,但没有找到任何可以帮助我解决问题的内容.

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

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

示例主布局文件:

<包括android:id="@+id/recent_gallery_layout_id"布局="@layout/recent_gallery_layout"android:layout_below="@id/running_gallery_layout_id"/><包括android:id="@+id/service_gallery_layout_id"布局="@layout/service_gallery_layout"android:layout_below="@id/recent_gallery_layout_id"/><包括android:id="@+id/process_gallery_layout_id"layout="@layout/process_gallery_layout"android:layout_below="@id/service_gallery_layout_id"/></RelativeLayout>

包含的合并文件示例:

<画廊android:id="@+id/service_gallery_id"android:layout_width="fill_parent"android:layout_height="wrap_content"机器人:layout_weight="1"android:layout_below="@id/service_gallery_title_text_id"/><文本视图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"/></合并>

我遇到了两个问题:

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

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

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

解决方案

include 标签存在问题.检查:https://issuetracker.google.com/issues/36908001

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

I have been trying for a few days now to make my layouts more efficient by converting from using several levels of nested LinearLayouts to one RelativeLayout and have come across a few problems that I haven not been able to find a workaround for...

I have searched the Android beginners group and this site and have not been able to find anything that would help me solve the problem.

I read on one of the blogs that you can combine layouts with merge and include tags. So what I have is a main layout file with a RelativeLayout root element. Inside of that I have 5 include tags that reference 5 different xml layout files that each have a merge element for the root (all of my merge files are the same except for the ids in them).

I am running into two problems, which I will explain after posting a simplified version of my layout code:

Sample Main Layout File:

<?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>

Sample included merge file:

<?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>

I am running into two problems:

1) The android:layout_* attributes seem to be ignored when used in the include tag and all of the merged layouts are displayed on top of each other. According to this post (http://developer.android.com/resources/articles/layout-tricks-reuse.html) "any android:layout_* attribute can be used with the <include /> tag"

2) Since I couldn't get this working I decided to try adding an android:layout_below attribute to the first TextView item in each merge layout file, meaning that each merge file would be referencing an id from another merge layout file... For the most part this actually worked and my layout looks fine. However, I get an error on one of the android:layout_below attributes saying that it can't find the id I specified... I have double and triple checked the ids to make sure they were correct. The weirdest part is that I used the AutoFill feature to put the id in the attribute in the first place.

If anyone has any suggestions or workarounds I will be more than happy to try them out. Also, if anyone can think of a way for me to just have one merge xml layout file instead of 5 that would be greatly appreciated. I couldn't find a way to do that because I need to have access to each item in the merge layout files at runtime...

解决方案

There is an issue with the include tag. Check: https://issuetracker.google.com/issues/36908001

To fix it, make sure you overwrite BOTH layout_width and layout_height when including, otherwise everything will be ignored.

这篇关于如何让RelativeLayout 与合并和包含一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆