Android:具有现有布局的自定义视图的多个视图子项

mre*_*elt 27 java xml android android-custom-view android-layout

我必须在Android中构建更复杂的自定义视图.最终布局应如下所示:

<RelativeLayout>
  <SomeView />
  <SomeOtherView />
  <!-- maybe more layout stuff here later -->
  <LinearLayout>
    <!-- the children -->
  </LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

但是,在XML文件中我只想定义它(不定义SomeView,SomeOtherView等):

<MyCustomView>
  <!-- the children -->
</MyCustomView>
Run Code Online (Sandbox Code Playgroud)

这在Android中是否可行,如果是的话:最干净的方法是什么?我想到的可能的解决方案是"覆盖addView()方法"和"删除所有视图并在以后再次添加它们",但我不确定要走哪条路......

非常感谢您的帮助!:)

Dev*_*red 53

绝对可以并且鼓励创建自定义容器视图.这就是Android所谓的复合控件.所以:

public class MyCustomView extends RelativeLayout {
    private LinearLayout mContentView;

    public MyCustomView(Context context) {
        this(context, null);
    }

    public MyCustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        //Inflate and attach your child XML
        LayoutInflater.from(context).inflate(R.layout.custom_layout, this);
        //Get a reference to the layout where you want children to be placed
        mContentView = (LinearLayout) findViewById(R.id.content);

        //Do any more custom init you would like to access children and do setup
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if(mContentView == null){
            super.addView(child, index, params);
        } else {
            //Forward these calls to the content view
            mContentView.addView(child, index, params);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以根据需要覆盖尽可能多的版本addView(),但最后它们都会回调我放在示例中的版本.覆盖此方法将使框架将其XML标记内的所有子项传递给特定的子容器.

然后修改XML:

RES /布局/ custom_layout.xml

<merge>
  <SomeView />
  <SomeOtherView />
  <!-- maybe more layout stuff here later -->
  <LinearLayout
      android:id="@+id/content" />
</merge>
Run Code Online (Sandbox Code Playgroud)

使用的原因<merge>是简化层次结构.所有子视图都将附加到您的自定义类,这是一个RelativeLayout.如果你不使用<merge>,你最终会RelativeLayout附加到另一个RelativeLayout附在所有孩子身上,这可能会导致问题.

HTH

  • NullPointerException是由inflate方法调用给定根视图上的addView(在本例中为this)和mContentView在调用addView时为null引起的.如果mContentView为null,可以通过调用super.addView来解决它... (2认同)