XML中定义的扩展LinearLayout的子代不显示

XML中定义的扩展LinearLayout的子代不显示

本文介绍了XML中定义的扩展LinearLayout的子代不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我的屏幕上重复了很多相同的图案-它基本上是一个视图容器,其中包含标题,图像和特定背景的线性布局.为避免多次复制和粘贴相同的序列,我认为我可以创建一个复合视图,扩展LinearLayout并在那里定义所有样式",然后在布局中使用该组件.我遵循了howto的示例,并使我的复合视图起作用.但是,我见过的所有示例都使用结果视图,如下所示:

In my project I have screens where the same pattern is repeated a lot - it's basically a container for views consisting of a linear layout with the heading, image and specific background. To avoid copying and pasting the same sequence multiple times I thought I could create a compound view, extend LinearLayout and define all the "styling" there, and then just use that component in my layouts.I followed howto's and examples and got my compound view to work. However, all examples I've seen use the resulting view as follows:

<com.myproject.compound.myview
    ...some attrs...
/>

即没有通过XML添加任何子级.我需要这样使用它:

I.e. no children are added via XML. I need to use it like this:

<com.myproject.compound.myview
    ...some attrs...>
    <TextView
        ..../>
    ...other views...

</com.myproject.compound.myview>

由于我正在扩展LinearLayout,因此我期望"myview"标签也可以像LinearLayout一样工作,但是由于某些原因,放置在我内部的项不会被绘制.为了绘制内部视图,我需要做些特别的事情吗?

Since I'm extending LinearLayout I was expecting "myview" tag to work like LinearLayout too, but for some reason items I put inside do not get drawn. Is there something I need to do specially to get the inner views to draw?

我扩展的LinearLayout非常简单,我没有重写任何方法,只是在构造函数中调用super并像这样夸大了布局:

My extended LinearLayout is very simple, I am not overriding any methods and just calling super in constructor and inflating the layout like this:

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.my_compound_view, this, true);

更新:我想我将添加XML作为参考点:

UPDATE: I thought I'd add an XML as a point of reference:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"
android:padding="12dp">
<TextView
    android:id="@+id/section_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#FF0000AA"        />
<ImageView
    android:layout_width="match_parent"
    android:layout_height="2dp"
        android:src="@drawable/line"        />
</LinearLayout>

推荐答案

实际上找到了一个更优雅的解决方案.只需在复合视图中使用合并标记而不是LinearLayout.全部归结为:

Actually found a more elegant solution. Just need to use merge tag instead of LinearLayout in the compound view. All boils down to:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    <TextView
        android:id="@+id/section_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="HEADING"
        android:textColor="#FF0000AA"        />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:src="@drawable/line"        />
</merge>

public class CompoundLayout extends LinearLayout{
    public CompoundLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = (LayoutInflater) context
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.compound_layout, this, true);
    }
}

主要布局:

<com.testbench.CompoundLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFDDEE"
    android:orientation="vertical">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Inner text"
            android:layout_gravity="center_horizontal"/>
</com.testbench.CompoundLayout>

这篇关于XML中定义的扩展LinearLayout的子代不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:21