java - 无法显示我的自定义 Android View

标签 java android

我正在尝试创建一个自定义控件,它除了装饰显示的文本外什么都不做。当我像我正在阅读的所有示例一样在 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/

相关文章:

java - 处理小文件mapreducehadoop

java - Eclipse 中的“API 工具 Javadoc 建议”问题

java - Micronaut HttpResponse 主体 CompositeByteBuf 类型抛出 io.netty.util.IllegalReferenceCountException : refCnt: 0

android - React-native:在 Android 中不会调用切换组件 onValueChange

android - GLES10.glGetIntegerv 在三星设备上返回 0

java - 将datetimeString转换为毫秒,返回负值?

java - xml布局的问题

java - SSL异常 : Could not find any key store entries to support the enabled cipher suites

android - dumpsys gfxinfo 报告中的 "Total frames"和 "Janky frames"

android - 显示 ListView 时在后台下载图像