难道Android的XML布局的'包括'标签真的有用吗? [英] Does Android XML Layout's 'include' Tag Really Work?

查看:112
本文介绍了难道Android的XML布局的'包括'标签真的有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法用&LT时覆盖属性;包括>在我的Andr​​oid的布局文件。当我搜索的错误,我发现拒绝发行2863

I am unable to override attributes when using <include> in my Android layout files. When I searched for bugs, I found Declined Issue 2863:

包括标签坏了(覆盖布局PARAMS从未工程)

"include tag is broken (overriding layout params never works)"

由于罗曼表示这部作品在测试套件和他的例子,我一定是做错了什么。

Since Romain indicates this works in the test suites and his examples, I must be doing something wrong.

我的项目是这样组织的:

My project is organized like this:

res/layout
  buttons.xml

res/layout-land
  receipt.xml

res/layout-port
  receipt.xml

该buttons.xml包含了这样的事情:

The buttons.xml contains something like this:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

  <Button .../>

  <Button .../>
</LinearLayout>

和在纵向和横向receipt.xml文件看起来是这样的:

And the portrait and landscape receipt.xml files look something like:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

  ...

  <!-- Overridden attributes never work. Nor do attributes like
       the red background, which is specified here. -->
  <include
      android:id="@+id/buttons_override"
      android:background="#ff0000"
      android:layout_width="fill_parent"
      layout="@layout/buttons"/>

</LinearLayout>

我在想什么?

What am I missing?

推荐答案

我刚刚发现的问题。首先,你只能覆盖layout_ *属性,因此背景将无法正常工作。这是记录在我的部分行为,只是一时疏忽。

I just found the issue. First, you can only override layout_* attributes, so the background won't work. That is documented behavior and simply an oversight on my part.

真正的问题是在LayoutInflater.java发现:

The real problem is found in LayoutInflater.java:

// We try to load the layout params set in the <include /> tag. If
// they don't exist, we will rely on the layout params set in the
// included XML file.
// During a layoutparams generation, a runtime exception is thrown
// if either layout_width or layout_height is missing. We catch
// this exception and set localParams accordingly: true means we
// successfully loaded layout params from the <include /> tag,
// false means we need to rely on the included layout params.
ViewGroup.LayoutParams params = null;
try {
   params = group.generateLayoutParams(attrs);
} catch (RuntimeException e) {
   params = group.generateLayoutParams(childAttrs);
} finally {
   if (params != null) {
     view.setLayoutParams(params);
   }
}

如果在&lt;&包括GT;标签不包括两个 layout_width和layout_height的RuntimeException的发生,并自动处理,没有任何日志声明偶数。

If the <include> tag does not include both layout_width and layout_height, the RuntimeException occurs and is silently handled, without any log statement even.

解决方案是将总是包括layout_width和layout_height时使用的&lt;&包括GT;标签,如果你想覆盖的任何layout_的*属性。

The solution is to always include both layout_width and layout_height when using the <include> tag, if you want to override any of the layout_* attributes.

我的例子应该更改为:

<include
      android:id="@+id/buttons_override"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      layout="@layout/buttons"/>

这篇关于难道Android的XML布局的'包括'标签真的有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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