我想创建一个自定义 View 在Android上。我试图尽可能简单,并创建一个几乎空的班级 MyView 并在我的 LinearLayout 但是该应用程序一开始就以“力关闭”而失败。我该如何做一个简单的自定义 View?根据 构建自定义组件View 如果我不覆盖,请获得100x100的尺寸 onMeasure().

public class MyView extends View {

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

我在一个 LinearLayout 和:

<view
    class="com.example.MyView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0" />

我究竟做错了什么?


如果我使用构造函数 itemon 建议和对超级类的相应调用。然后“力量关闭”消失了,但是我 LinearLayout 被打破了,之后的组件 MyView 未显示。

这是我的 main.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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0"
    android:background="#f00"
    android:text="Hello"
/>
<view
    class="com.example.MyView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0"
/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0"
    android:background="#00f"
    android:text="World"
/>
</LinearLayout>
有帮助吗?

解决方案

可能是您可以定义另一种构造函数方法:

public MyView(Context context, AttributeSet attrs)

Android框架将尝试通过上面的构造函数来构建UI。

其他提示

Android开发人员指南有一个称为“构建自定义组件”的部分。不幸的是,XML属性的讨论仅涵盖在布局文件中声明控件,而不是实际处理类初始化中的值。步骤如下:

在值 attr.xml中声明属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCustomView">
        <attr name="android:text"/>
        <attr name="android:textColor"/>            
        <attr name="extraInformation" format="string" />
    </declare-styleable>
</resources>

请注意,在声明类别标签中使用不合格的名称。非标准的Android属性(例如外形信息)需要声明其类型。超级类别中声明的标签将以子类别提供,而无需重新申请。

创建构造函数

由于有两个构造函数使用属性集进行初始化,因此为构造函数创建单独的初始化方法很方便。

private void init(AttributeSet attrs){  
    TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.MyCustomView);
    //Use a
    Log.i("test",a.getString(R.styleable.MyCustomView_android_text));
    Log.i("test",""+a.getColor(R.styleable.MyCustomView_android_textColor, Color.BLACK));
    Log.i("test",a.getString(R.styleable.MyCustomView_android_extraInformation));
    //Don't forget this
    a.recycle();
}

R.Stylable.MyCustomView是一种自动化INT []资源,其中每个元素都是属性的ID。通过将属性名称附加到元素名称中,为XML中的每个属性生成属性。然后可以使用各种GET函数从打字机中检索属性。如果未在XML中定义属性,则返回NULL。当然,除了返回类型是原始类型,在这种情况下,第二个参数将返回。

如果您不想检索所有属性,则可以手动创建此数组。标准Android属性的ID包含在Android.r.attr中,而该项目的属性则在R.Attr中。

int attrsWanted[]=new int[]{android.R.attr.text, R.attr.textColor};

请注意,您不应在Android.r.Stylable中使用任何内容,因为该线程将来可能会改变。在文档中,它仍然是在一个地方查看所有这些常数是有用的。

在布局文件中使用它,例如Layout main.xml包括名称空间声明

XMLNS:app =“ http://schemas.android.com/apk/res/com.mycompany.projectName”

在顶级XML元素中。

<com.mycompany.projectname.MyCustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:text="Test text"
    android:textColor="#FFFFFF"
app:extraInformation="My extra information";
/> 

使用完全合格的名称引用自定义视图。

Android LabelView样品

如果您想要一个完整的示例,请查看Android标签视图示例。

labelview.java

TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.LabelView);
CharSequences=a.getString(R.styleable.LabelView_text);
attrs.xml

<declare-styleable name="LabelView">
    <attr name="text"format="string"/>
    <attr name="textColor"format="color"/>
    <attr name="textSize"format="dimension"/>
</declare-styleable>

custom_view_1.xml

<com.example.android.apis.view.LabelView
    android:background="@drawable/blue"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:text="Blue"app:textSize="20dp"/>

这包含在带有名称空间属性的线性层压中:

XMLNS:app =“ http://schemas.android.com/apk/res/com.example.android.apis”

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top