Android:是否有任何好的资源可以用于构建不带 XML 的仅 Java 视图

发布于 2024-12-20 14:21:00 字数 1582 浏览 3 评论 0原文

我已经浏览了所有文档以及网上找到的示例,但我还没有找到任何关于仅使用 Java 构建自定义视图的好资源。我见过许多描述如何使用 XML 和 Java 创建它们的内容。

我认为我最困扰的事情是相对简单的事情,例如“我可以在自定义视图中使用“TextView”小部件并使用画布绘制一些图形吗?我已经成功地做了一个或其他但似乎无法找到同时执行这两项操作的方法,

例如:我希望在视图顶部有一个 TextView 小部件,其下方有一个可触摸的图形,充当表盘并绘制在占据画布的画布上。添加 TextView 后的所有可用空间。 dial,它会增加 TextView 内的文本,我认为类似以下的内容会起作用......但它不起作用,很可能是因为我错过了一些简单的东西。

 public class Dial extends LinearLayout implements View.OnTouchListener
{
    Bitmap _dial;
    protected int bpm = 120;
    TextView txtBpm;    

    /**
     * Constructor
     * @param context
     */
    public Dial(Context context)
    {
        super(context);
        _dial =  BitmapFactory.decodeResource(getResources(), R.drawable.dial_small);
        txtBpm = new TextView(context);
        txtBpm.setTextSize(43);
        txtBpm.setWidth(100);
        txtBpm.setText(String.valueOf(bpm));
        this.addView(txtBpm);
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
           // Setting the y-pos to 160 so even
           // if the canvas fills the view,
           // the dial will still be below the textview.
        canvas.drawBitmap(_dial, 0,160,null); 
    }   
    @Override
    public void onSizeChanged(int w, int h, int oldW, int oldH)
    {

    }
    /* (non-Javadoc)
     * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
     */
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1)
    {
        // Do some stuff here to rotate the dial
        return false;
    }

}

I've been going through all of the documentation, and examples found on the web, but I have yet to find any good resources on building custom views using Java ONLY. I have seen many that describe about creating them using XML and Java.

I think the thing that I'm struggling with the most are relatively simple things like, "can I use a "TextView" widget in a custom view AND also use the canvas to draw some graphics? I've been successful doing one or the other but can't seem to find ways to do both simultaneously.

For example: I would like to have an TextView widget at the top of a view with a touchable graphic underneath that acts as a dial and is drawn onto the canvas which takes up all available space once the TextView is added. As I turn the dial, it increments text inside the TextView. I would think something like the following would work... but it doesn't and it most likely because I'm missing something simple.

 public class Dial extends LinearLayout implements View.OnTouchListener
{
    Bitmap _dial;
    protected int bpm = 120;
    TextView txtBpm;    

    /**
     * Constructor
     * @param context
     */
    public Dial(Context context)
    {
        super(context);
        _dial =  BitmapFactory.decodeResource(getResources(), R.drawable.dial_small);
        txtBpm = new TextView(context);
        txtBpm.setTextSize(43);
        txtBpm.setWidth(100);
        txtBpm.setText(String.valueOf(bpm));
        this.addView(txtBpm);
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
           // Setting the y-pos to 160 so even
           // if the canvas fills the view,
           // the dial will still be below the textview.
        canvas.drawBitmap(_dial, 0,160,null); 
    }   
    @Override
    public void onSizeChanged(int w, int h, int oldW, int oldH)
    {

    }
    /* (non-Javadoc)
     * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
     */
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1)
    {
        // Do some stuff here to rotate the dial
        return false;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文