【问题标题】:How to create a custom view with nested xml as the content如何创建以嵌套 xml 为内容的自定义视图
【发布时间】: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 的直接子级,位于现有 TextViewView 之间;或将内容TextView 移动到已经定位在那里的FrameLayout。如果是第一个,那么你只需要在onFinishInflate() 中调整TextViewLayoutParams 的内容。如果是第二个,您必须展示完整的当前布局,并说明您的代码如何无法按预期运行。

标签: android android-custom-view custom-view


【解决方案1】:

为什么不使用&lt;Merge&gt; 而不是&lt;Include&gt;

所以把你的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" />

阅读更多关于Re-using layouts with Include tag

膨胀包含的布局

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");

【讨论】:

    猜你喜欢
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2014-02-26
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    相关资源
    最近更新 更多