Seaching模式删除的ViewGroup子类不需要布局子类、不需要、布局、模式

2023-09-06 14:39:31 作者:基霸

由于我想要的LinearLayout的子类的一些像这样的:

Given I want some subclassing of a LinearLayout like this:

public class MyLayout extends LinearLayout {

    public int someState=0;

    public ListViewHeadView(Context context) {
        super(context);
        View.inflate(context, R.layout.some_layout, this);
    }

    // more code here
}

像这样some_layout:

with some_layout like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
                style="@style/textStyle"
                android:id="@+id/someTextView"/>

        <Button
                style="@style/buttonStyle"
                android:id="@+id/someButton"/>
    </LinearLayout>

这工作得很好,但我有一个的LinearLayout得多花费的性能。我目前看到的唯一的方法来消除不需要的的LinearLayout是使用&LT;合并&GT; 的XML,而不是里面的LinearLayout。但我会做一个setOrientation在我的code,我很想在一个XML看到(一次 - 不是在所有用途)。可有人点我就如何做到这一点很好的模式?如果我没有一个国家,我可以使用装饰图案,只是装饰用我需要的功能的LinearLayout - 但是当我在这个布局需要的状态我看没有什么好办法呢。任何提示和最佳实践的欢迎!

this works fine, but I have one LinearLayout to much which costs performance. The only way I see at the moment to remove the unneeded LinearLayout is to use a <merge> inside the xml instead of the LinearLayout. But then I would have to do a setOrientation in my code which I would love to see in an xml ( once - not at all usages ). Can somebody point me to a pattern on how to do this nicely? If I do not have a state I can use the decorator-pattern and just decorate the LinearLayout with the functions I need - but when I need a state in this Layout I see no good way yet. Any hints and best practices welcome!

推荐答案

我定义了的android:定位在项目的 ATTRS 属性。 XML 为您定制的ViewGroup ,并添加一些code在的ViewGroup 的构造。

I'd define the android:orientation attribute in your project's attrs.xml for your custom ViewGroup and add some code to evaluate this in the ViewGroups constructor.