【问题标题】:Android CustomView is shown twiceAndroid CustomView 显示两次
【发布时间】:2016-03-10 12:58:39
【问题描述】:

我创建了一个添加到 XML 布局中的 CustomView。 这都是一个非常简单的布局,不应该显示任何问题。尽管如此,令我恼火的是,某些元素似乎被添加到布局中(在其自身中)两次。

XML 如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/activityLayoutRL">

    <world.b2g.b2gether.ui.RadarQuickInfo
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radarDetailView"
        android:visibility="visible"/>

</RelativeLayout>

视图本身只是扩展了一个 LinearLayout 并像这样初始化:

 public void initView(Context context){
    this.context = context;
    LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.radar_detail_layout, this);
}

切换到另一个版本号时,它有时似乎会发生变化。我为 API 级别 23 开发,但有时更改为 22 似乎可以解决它(至少有时)

所以我创建了这个构造函数

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RadarQuickInfo(Context context, AttributeSet attrs, 
    int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initView(context);
}

自定义视图的布局似乎没有任何改变。所以即使这样也会产生这种效果:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/radarDetailRootViewLL">

</LinearLayout>

我想,某处可能有错误? 有什么想法吗?

【问题讨论】:

  • 你能告诉我们你的radar_detail_layout吗?
  • 即使布局为空也会发生。但可以肯定的是,我为你编辑了这篇文章。谢谢!
  • 你能提供完整的initView()吗?
  • initView 只是初始化所有视图元素的一种方法。即使我将其保留为空并仅存储上下文,我也会遇到这个问题。
  • 你是如何膨胀包含你的自定义视图的 xml 的?

标签: android xml android-custom-view


【解决方案1】:

&lt;merge&gt;包裹你的自定义视图的布局

【讨论】:

  • 好吧,我尝试了很多,但这似乎仍然没有帮助。无论如何,谢谢。
  • 合并标签后,您需要删除布局xml中的根元素。
猜你喜欢
  • 1970-01-01
  • 2022-01-03
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多