我一直在尝试了几天,现在使通过使用嵌套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/物品/布局花招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

要解决这个问题,确保包括您覆盖BOTH layout_widthlayout_height,否则一切都将被忽略。

其他提示

查看更高度投下面的答案。该矿可悲过时


我可以解决一个问题的贾斯汀提出:RelativeLayout的的无法管理的定位包括(至少在这个简单的例子,在一个1.6仿真器)

<强> CommonsWare 建议包裹包括在一个独特的父容器,但为了帮助解决&确定范围内相同的命名视图这样做的 Justin的包括

  

每个将必须具有一个   唯一父容器,你会   调用findViewById(),该容器上   (ViewGroup中),而不是在   活性。

其实,你的也必须这样做的为了得到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" />
    <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>

在裸页脚包括不会对准以母体的底部,而周围的LinearLayout ..我不会把此预期行为。

此外,web视图似乎本身很好地附着到由ID,但相信这是幻觉,由于它简单地流过的标题下方垂直。我也试着设置页脚上方的按钮权包括但它得到了所有轻飘与错,太

的RelativeLayout有更多的问题在1.5,但我仍然喜欢它:)

男人,这是旧的,但它似乎要拿出在搜索的顶部,所以我要评论。

我认为这里的诀窍在于,<merge>标签与标签<include>合并在该水平基本上除去任何种类的“父”视图组。那么,到底谁是你问到“layout_below”别人呢?没有人。有在该水平没有图。

<merge>标签有孩子的意见,并将其持久性有机污染物对入<include>标签的父。因此,必须要求孩子在布局你包括锚自己相应。

有关定位工作在RelativeLayout的需要设置layout_ *参数中包含文件,而不是在主布局文件。这样

main_layout.xml     

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

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

唯一的解决办法
  

在机器人:layout_ *属性似乎   在包括使用时被忽略   标签和所有的合并布局   在彼此的顶部显示。

我的猜测是,你不能参考,从布局规则,被上android:id元件限定<include>属性,只有那些是上的“真正的”微件和容器。

  

此外,如果任何人都可以想办法对   我只是有一个合并XML布局   文件,而不是5,这将是   不胜感激。

简单:把他们都在一个文件

  

我无法找到一个方法来做到这一点   因为我需要访问每个   在合并项目布局文件在   运行

无论你是否有一个<include>元或1000,所有的内容应该是在运行时访问。一个例外是,如果你有重复的android:id属性 - 你需要适当范围的findViewById()电话,以获得正确的,就像你从一个ListView行越来越小部件时

如果您可以创建一个示例项目,用途2+合并文件,你可以证明内容的的在运行时可以访问,让我知道。

在我的情况,我试图包括与<merge标签开始布局。当我把它改成一个布局,说<RelativeLayout它的工作。下面是说明。

WORKING

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

我有同样的问题,甚至定义layout_widthlayout_height没有奏效。 问题是,布局我有包括标签和删除它们之后,一切工作就像一个魅力。 我想,合并不是一个布局标签正因为如此,它不能接收定位和尺寸参数。既然你在被转移到内部布局父定义的一切,设置只是被扔掉。

TL:DR:只是去掉标签,移动的xmlns定义,以真正的布局viewholder,你应该是好

在:

<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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top