【发布时间】:2018-10-20 12:01:50
【问题描述】:
在我的布局中定义控件时,我在创建自定义视图组件然后显示嵌套的 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 创建了一个派生类,我可以得到属性
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);
}
}
}
}
谢谢
【问题讨论】:
-
目前尚不清楚哪种方法是您当前的方法 - 将内容
TextView作为SonrCard的直接子级,位于现有TextView和View之间;或将内容TextView移动到已经定位在那里的FrameLayout。如果是第一个,那么你只需要在onFinishInflate()中调整TextView的LayoutParams的内容。如果是第二个,您必须展示完整的当前布局,并说明您的代码如何无法按预期运行。
标签: android android-custom-view custom-view