我正在尝试创建一个自定义控件,它除了装饰显示的文本外什么都不做。当我像我正在阅读的所有示例一样在 main.xml 中引用此类并将其部署到模拟器时,小部件只会显示“加载小部件时出现问题”。
我正在开始 Android 开发,只是想了解如何开发自定义控件:
这是 ExtendedTextView 控件:
package uk.co.andrewrea.android;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class ExtendedTextView extends TextView {
public ExtendedTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ExtendedTextView(Context context, AttributeSet attrs){
super(context,attrs);
}
public ExtendedTextView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
}
@Override
public CharSequence getText() {
// TODO Auto-generated method stub
return "***" + super.getText() + "***";
}
}
这是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:orientation="vertical"
android:background="@drawable/widget_bg_normal"
android:layout_gravity="center"
android:layout_height="wrap_content">
<uk.co.andrewrea.android.ExtendedTextView android:id="@+id/widget_textview"
android:text="@string/hello"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal|center"
android:layout_marginTop="5dip"
android:padding="10dip"
android:textColor="@android:color/black"/>
</LinearLayout>
干杯,
安德鲁
最佳答案
When I reference this class inside the main.xml like all the examples which I am reading and deploy this to the emulator, the widget just says "Problem Loading Widget."
此消息仅针对主屏幕应用程序小部件显示。如果那是您要构建的内容,则不能将自定义 View
类与应用程序小部件一起使用。您只能将自定义 View
类与 Activities
一起使用。
关于java - 无法显示我的自定义 Android View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3080144/