java - 将编辑文本添加到自定义 View 中

标签 java android

我有一个自定义 View ,看起来有点像速度计。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:slider="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<com.mysite.view.SeekBar_Speedometer
    android:id="@+id/slider"
    slider:fill_clockwise_color="@color/lightgrey"
    slider:fill_anticlockwise_color="@color/blue"
    slider:fill_handle_color="@color/green"
    slider:fill_handle_width="3"
    slider:angle_start="120"
    slider:angle_end="60"
    slider:fill_start_angle="120"
    slider:score_min="0"
    slider:score_max="300"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

我想在速度计的中心添加一个编辑文本。

正如您可以在 Canvas 上绘制文本一样,我可以在 View 中以相同的方式添加编辑文本吗?我不这么认为!

问题是,如何将此编辑文本添加到我的自定义 View 中?

最佳答案

在自定义 View 类中,您可以执行以下操作:

EditText edit = new EditText(context);
addView( edit );

为此,您的自定义 View 必须扩展 ViewGroup(LinearLayout、RelativeLayout、ecc..)

关于java - 将编辑文本添加到自定义 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11313130/

相关文章:

java - jobRepository 是否必须使用与 JdbcBatchItemWriter 相同的事务管理器?

java - 我应该使用 ConcurrentHashMap 吗?

android - 将元素添加到列表时出现 UnsupportedOperationException(无 Arrays.asList)

java - 数组长度 20 但我希望它在读取最后一个节点后就中断

java - 以编程方式确定泛型类型

android - 获取所有 ListView 项目(行)

android - 当应用程序处于后台或被杀死时 FCM onMessageReceived() 未调用

适用于 x86_64 的 Android NDK 没有 bcopy 和索引引用

android - 如何在android中的webview中打开桌面站点

java - 对 Serialized 的内部工作原理感到困惑