代码之家  ›  专栏  ›  技术社区  ›  Justin

如何使用合并和包含进行相对布局?

  •  113
  • Justin  · 技术社区  · 15 年前

    我已经尝试了几天,通过从使用多个层次的嵌套转换来提高布局的效率。 LinearLayouts 对一 RelativeLayout 我遇到了一些问题,但我找不到解决方法…

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

    我在其中一个博客上读到,你可以将布局与合并和包含标签相结合。所以我有一个主布局文件 相对布局 根元素其中,我有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_* 当在include标记中使用时,属性似乎被忽略,并且所有合并的布局都显示在彼此的顶部。根据这篇文章( http://developer.android.com/resources/articles/layout-tricks-reuse.html ) 安卓:布局_* 属性可用于 <include /> “标签”

    2)由于我无法完成这项工作,我决定尝试添加 android:layout_below 属于第一个 TextView 每个合并布局文件中的项,这意味着每个合并文件将引用另一个合并布局文件中的ID…在大多数情况下,这确实有效,我的布局看起来很好。但是,我在 安卓:布局图 属性表示它找不到我指定的ID…我检查了两次和三次身份证以确保它们是正确的。最奇怪的是我用了 AutoFill 将ID放在属性中的特性。

    如果有人有任何建议或解决方法,我会非常乐意尝试。另外,如果有人能为我提供一个合并XML布局文件而不是5的方法,我会非常感激。我找不到这样做的方法,因为我需要在运行时访问合并布局文件中的每个项…

    7 回复  |  直到 7 年前
        1
  •  211
  •   Cœur Gustavo Armenta    8 年前

    包含标签有问题。检查: https://issuetracker.google.com/issues/36908001

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

        2
  •  32
  •   alienjazzcat    11 年前

    请看下面投票率更高的答案。我的已经过时了


    我可以解决一个问题 贾斯廷 引发:相对布局无法管理include的定位(至少在这个简单的例子中,在1.6模拟器上)

    公用SWARE 建议将include包装在一个唯一的父容器中,但这样做是为了帮助在 贾斯廷 包括

    每个人都必须有一个 唯一的父容器,您将 在该容器上调用findViewByID()。 (视图组)而不是 活动。

    事实上,你 也必须这样做 为了使相对布局按预期工作:

    这作品( 页脚 位置正确):

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <include android:id="@+id/header" layout="@layout/header"
            android:layout_alignParentTop="true" />
        <WebView android:id="@+id/webView" android:layout_below="@id/header"
            android:background="#77CC0000" android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:focusable="false" />
        <LinearLayout android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" android:layout_width="fill_parent">
            <include android:id="@+id/footer" layout="@layout/footer" />
        </LinearLayout>
    </RelativeLayout>
    

    这不 页脚 在屏幕顶部浮动):

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <include android:id="@+id/header" layout="@layout/header"
            android:layout_alignParentTop="true" />
        <WebView android:id="@+id/webView" android:layout_below="@id/header"
            android:background="#77CC0000" android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:focusable="false" />
        <include android:id="@+id/footer" layout="@layout/footer"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>
    

    光秃秃的 页脚 包含将不与父级底部对齐,没有周围的线性布局。我不认为这是预期的行为。

    此外,WebView似乎可以很好地附加到 页眉 根据ID,但我认为这是一种错觉,因为它只是垂直地在标题下面流动。我还试图在页脚上方设置一个按钮include,但它也是浮动的和错误的。

    相对布局在1.5中有更多的问题,但我仍然喜欢它:)

        3
  •  7
  •   Plantage    12 年前

    伙计,这是旧的,但它似乎在搜索的顶端,所以我要评论一下。

    我想这里的诀窍是 <merge> 标签与 <include> 标记实质上删除了该级别上的任何类型的“父”视图组。那么,你到底是想让谁把“布局”放在别人的下面呢?没有人。在那个级别上没有视图。

    这个 <合并; 标记获取子视图并将其直接弹出到 <包括> 标签。因此,您必须要求布局中的孩子们相应地锚定自己。

        4
  •  4
  •   Maragues    13 年前

    要定位以进行相对布局,您需要在include文件中而不是在主布局文件中设置布局参数。那样

    MIN

    <RelativeLayout
      android:id="@+id/header"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
       ....
    </RelativeLayout>
    
    <RelativeLayout 
      android:id="@+id/footer"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true">
        .....
    </RelativeLayout>
    
    <include layout="@layout/content_layout" />
    

    内容\布局.xml

    <merge xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer"
        android:layout_below="@id/header" >
    
        ....
    </RelativeLayout>
    </merge>
    

    这显然不是美国开发人员想要的,但它是我找到的唯一避免复制XML的解决方案。

        5
  •  1
  •   CommonsWare    15 年前

    Android:布局属性似乎 在include中使用时被忽略 标记和所有合并的布局 显示在彼此的顶部。

    我猜你不能从布局规则中引用, android:id 在上定义的属性 <include> 元素,只有那些在“真正的”小部件和容器上的元素。

    还有,如果有人能想出一个方法 我只有一个合并XML布局 文件而不是5 非常感谢。

    简单:把它们放在一个文件中。

    我找不到办法 因为我需要每个人 合并布局文件中的项 运行时

    不管你有没有 <包括: 元素或1000,所有内容都应该在运行时可访问。一个例外是如果你复制了 Android:身份证 属性——您需要正确地确定 findViewById() 调用以获取正确的控件,就像从ListView行获取小部件一样。

    如果可以创建一个使用2+合并文件的示例项目,则可以在其中演示内容 运行时可访问,请通知我。

        6
  •  0
  •   Rohit Mandiwal    8 年前

    在我的例子中,我试图包括的布局从 <merge 标签。当我把它改成布局时,比如说 <RelativeLayout 它奏效了。下面是插图。

    工作

    <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        tools:showIn="@layout/activity_home">
    

    不工作

    <merge xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        tools:showIn="@layout/activity_home">
    
        7
  •  0
  •   silvio.pereira    7 年前

    我也有同样的问题,甚至定义 layout_width layout_height 它不起作用。 问题是,我所包含的布局有标签,在移除它们之后,一切都像一个魅力。 我认为merge不是布局标记,因此它不能接收定位和大小参数。因为您在中定义的所有内容都被传输到内部父布局,所以设置被丢弃。

    tl:dr:只需移除标签,将xmlns定义移动到一个真正的布局视图框中,就可以了。

    之前:

    <merge
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <...ui.component.BorderCardView
            android:layout_width="112dp"
            android:layout_height="32dp"
            app:cardCornerRadius="4dp"
            app:cardUseCompatPadding="true">
    
            <ImageView
                android:layout_width="16dp"
                android:layout_height="16dp"
                android:src="@drawable/ic_logout"
                android:tint="@color/divider" />
    
        </...ui.component.BorderCardView>
    </merge>
    

    工作:

    <...ui.component.BorderCardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="112dp"
        android:layout_height="32dp"
        app:cardCornerRadius="4dp"
        app:cardUseCompatPadding="true">
    
        <ImageView
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:src="@drawable/ic_logout"
            android:tint="@color/divider" />
    
    </...ui.component.BorderCardView>