Adding text view to surface view

2019-07-02 20:58发布

i use this a s surface view in my application and now i want to add text view to this programmatically. How can i do that.

"<com.csfcse.udrawer.UdrawerDrawingSurface
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"
  android:id="@+id/drawingSurface" 
        android:layout_gravity="left" />"

Thanks......

3条回答
爷、活的狠高调
2楼-- · 2019-07-02 21:36

This is not possible, a SurfaceView cannot contain other Views.

查看更多
虎瘦雄心在
3楼-- · 2019-07-02 21:42

You'll need to use the Canvas and a corresponding text draw method:

drawText(String text, int index, int count, float x, float y, Paint paint).

查看更多
叼着烟拽天下
4楼-- · 2019-07-02 21:52

An option may be to have a FrameLayout with two children, first the SurfaceView, then whatever you want to overlay on the SurfaceView. In the sample below, the second View in the FrameLayout is a horozontil LinearLayout with a Button and a TextView. The FrameLayout displays all its children as piled in Z order with the first child at the bottom and all children positioned at the upper left corner. In this case the LinearLayout has Gravity.CENTER_VERTICAL (I think you could do the same thing with padding on the LinearLayout.

I have never found much (good) documentation about how SurfaceViews (or any of the screen drawing) works. There may be a problem with flickering or refresh, but I don't see much problem on my Froyo Evo. (This test app draws 'twirling' lines on the SurfaceView below the Button and TextView.

SurfaceView with views overlaying it

If the question is simply: How do you programmatically add a TextView to a Layout that was created by a XML inflated Layout, then, get a reference to the Layout instance and call addView() to it.

Layout lay = parentLayo.findViewById(R.id.drawingSurfaceParent);
TextView tv = new TextView(this);
tv.setText("New textview");
lay.addView(tv);
查看更多
登录 后发表回答