如何创建以嵌套 xml 作为内容的自定义视图



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

我尝试迭代子元素,只获取嵌套元素,然后使用 removeChild 然后添加 Child 将控件放入嵌套控件中,但这不起作用。

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

在我的活动.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>

合并.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);
}
}
}

}

谢谢

而不是<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");

最新更新