Android的 - 创建的TextView(或EditText上)编程和设置在屏幕坐标给出一个特定的地方 [英] Android - create TextView (or EditText) programmatically and set it in a specific place on the screen giving coordinates

查看:307
本文介绍了Android的 - 创建的TextView(或EditText上)编程和设置在屏幕坐标给出一个特定的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个Android应用程序,用户在屏幕上绘制某些对象。一种类型的对象是一个文本对象。用户通过拖动他的手指创建对象和对象被示出为可以移动/整形矩形。当用户点击文本对象,我推出了新的活动,其中用户进入我的onActivityResult方法返回文本。

I am building an android app where the user paints some objects in the screen. One type of object is a Text object. The user creates the object by dragging his finger and the object is shown as a rectangle that can be moved/reshaped. When user taps the text object, I launch a new activity where user enters text which I return on the onActivityResult method.

现在我要显示在对象中的文本。我可以有机会获得的东西,矩形等从我的文字类的坐标。我想做的事情在本质上是创建一个TextView(或EditText上)编程,并设置其边界为我矩形的,我的目标是画的界限。有没有一种方法,可以帮助我做到这一点?

Now I want to show the text in the object. I can have access to stuff as the coordinates of the Rectangle etc from my text class. What I want to do in essence, is to create a TextView (or EditText) programmatically and set its bounds as the bounds of my rectangle that my object is painted in. Is there a method that can help me do it?

(另一种方法是使用canvas.drawTextOnPath方法在我的文本对象,但这似乎因为我的文字可能会走出对象更加复杂,而我也必须处理多线)

(another approach would be to use the canvas.drawTextOnPath method in my text object. But this seems more complicated since my text might get out of the object, and I would also have to handle the multilines)

非常感谢你提前!

编辑:尝试GAMA的做法

trying GAMA's approach

protected void onActivityResult(int requestCode, int resultCode, Intent data) {                 
  switch(requestCode) { 
  case 1:
      if (resultCode == Activity.RESULT_OK) { 
            String text=data.getStringExtra("text");
            System.out.println(text);
            TextView tv=new TextView(this);
            //LayoutParams lp = new LayoutParams(new ViewGroup.MarginLayoutParams((int)texts.get(index).width,(int)texts.get(index).height));
            LayoutParams lp = new LayoutParams(new ViewGroup.MarginLayoutParams(100,100));
            //tv.setLayoutParams(lp);
            //lp.setMargins((int)texts.get(index).Sx, (int)texts.get(index).Sy, (int)texts.get(index).Lx, (int)texts.get(index).Ly);

            tv.setLayoutParams(lp);
            tv.setTextSize(10);
            tv.setTextColor(Color.RED);
            tv.setText(text);
            lp.setMargins(0,0,0,0);
            //tv.setVisibility(View.VISIBLE);
            System.out.println("got "+tv.getText());
            }
      break;
      }
  }  

无论打印显示的文本预期,但我没有看到在屏幕上的任何(试图在底部设置它留下开始)

both prints show the text as expected but I do not see anything in the screen (tried to set it on the bottom left to begin with)

推荐答案

其实, drawTextOnPath 是你最好的选择。它不会渗出。所有你需要做的是建立在正确的中心垂直,它从左边的一个路径,中心垂直的矩形。该方法将照顾调整大小的文字,这样它会留在路径内

Actually, drawTextOnPath is your best bet. It won't bleed out. All you have to do is create a path that goes from the left, center vertical of your rectangle to the right center vertical. The method will take care of resizing the text so that it will stay within the Path.

您可以通过使用相应地调整路径 Paint.getTextWidth()。如果宽度大于框,延长你的路径与行 Paint.getTextHeight()下面的第一行。

You can adjust the path accordingly by using Paint.getTextWidth(). If the width is bigger than the box, extend your path with a line Paint.getTextHeight() below the first line.

这篇关于Android的 - 创建的TextView(或EditText上)编程和设置在屏幕坐标给出一个特定的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆