我正在尝试创建一个可重复使用的控件,我可以在已经存在的控件中添加任何需要的地方,因此我不需要属性,无需添加。

当我将“自定义视图”拖动到布局时,我只希望控件显示我的视觉编辑器。

我很简单 view_textseek.xml 例如,如果我在3个不同的位置使用“文本和seekbar”时,我不想重新创建它的布局(例如,以下是:稍后颜色点)。或者只是具有“ TextView”和“ Seekbar”的控制

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

    <TextView
        android:id="@+id/view_textseek_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="15dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:text="@string/view_textseek_text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RelativeLayout
        android:id="@+id/view_textseek_container_seekbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/view_textseek_text"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="5dp" >

        <SeekBar
            android:id="@+id/view_textseek_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:max="255"
            android:progress="0" />
    </RelativeLayout>

</RelativeLayout>

这基本上是我的班级:

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

public class TextSeekView extends RelativeLayout
{

    public TextSeekView(Context context) { super(context); init(context); }
    public TextSeekView(Context context, AttributeSet attrs) { super(context, attrs); init(context); }
    public TextSeekView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); }

    protected void init(Context context)
    {
        if (!isInEditMode())
        {
            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layoutInflater.inflate(R.layout.view_textseek, this, true);
        }
    }
}

如您所见,我只想“收集”多个已经存在的控件,并具有一个“视图”或“控制”来处理这些控件。它没有显示在编辑中。

还是由于您必须正确加载“自定义”视图的某些旧类型的愚蠢错误,这是由于某种旧类型的愚蠢错误吗?

而且我不需要其他视图,根本没有任何特殊属性,我只想在添加它或任何其他布局时一次又一次地显示这种布局。

像C#一样,将3个文本框添加到一个控件中,只是您每次都使用3个文本框。然后,每当您想要3个文本框时将该控件拖出表单 - 仅此而已!

有帮助吗?

解决方案

更改此代码:

protected void init(Context context) {
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.view_textseek, this, true);

    if (!isInEditMode()) {
        // isInEditMode returns true when you show a view on graphical editor. Returns false while showing on running app.
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top