首页 > 解决方案 > 如何创建以嵌套 xml 为内容的自定义视图

问题描述

我在创建自定义视图组件然后在我的布局中定义控件时显示嵌套的 xml 内容时遇到问题。

我曾尝试迭代子项并仅获取嵌套元素,然后使用 removeChild 然后 addChild 将控件放入嵌套控件中,但这不起作用。

我有以下组件 SonrCard。这是一个相对布局,但我无法让孩子们显示在正确的位置。我想将 textview 注入到 Sonr Card 的内容区域

在我的 activity.xaml 中实现组件

<com.syntax.sonr.components.SonrCard
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        ads:title="@string/description">

                        <TextView
                            android:id="@+id/description_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="16dp"
                            android:layout_marginLeft="16dp"
                            android:layout_marginRight="16dp"
                            tools:text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede." />

                    </com.syntax.sonr.components.SonrCard>

merge.xml文件如下

    <merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">


    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:fontFamily="@font/open_sans_light"
        android:textSize="18sp"
        tools:text="Title" />

    // I would like to display the content (TextView) here


    <View
        android:id="@+id/border"
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:background="#c1c1c1" />

我已经从 RelativeLayout 创建了一个派生类,我可以得到 attrs

public class SonrCard extends RelativeLayout {

String _title;
TextView _titleTextView;
LinearLayout _card;
FrameLayout _content;

public SonrCard(Context context) {
    super(context);
}

public SonrCard(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);

    getAttributes(context, attrs);
    initializeView(context);
}

public SonrCard(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    getAttributes(context, attrs);
    initializeView(context);
}

public SonrCard(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    getAttributes(context, attrs);
    initializeView(context);
}

private void initializeView(Context context) {

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

private void getAttributes(Context context, AttributeSet attrs) {

    TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.SonrCard);
    _title = attributes.getString(R.styleable.SonrCard_title);

    attributes.recycle();
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    _card = findViewById(R.id.sonr_card);
    _titleTextView = findViewById(R.id.title);
    _titleTextView.setText(_title);
    _content = findViewById(R.id.content);

    final int count = _card.getChildCount();

    for (int i = 0; i < count; i++) {
        final View child = _card.getChildAt(i);
        int id = child.getId();

        if (id != R.id.title && id != R.id.border){
            _card.removeView(child);
            _content.addView(child);
        }
    }
}

}

谢谢

标签: androidandroid-custom-viewcustom-view

解决方案


<Merge>不是你为什么不使用<Include>

所以替换你merge.xml

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


<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    android:fontFamily="@font/open_sans_light"
    android:textSize="18sp"
    tools:text="Title" />

<include 
android:id="@+id/myIncludedLayout"
layout="@layout/yourSonrCardLayout"/>


<View
    android:id="@+id/border"
    android:layout_width="match_parent"
    android:layout_height="6dp"
    android:background="#c1c1c1" />

阅读更多关于重新使用包含标签的布局

膨胀包含的布局

View includedLayout = findViewById(R.id.myIncludedLayout);

// geting the description_text TextView declared in yourSonrCardLayout.xml by myIncludedLayout
TextView description_text= (TextView) myIncludedLayout.findViewById(R.id.description_text);
description_text.setText("this is header text 3");

推荐阅读