在TextView文字之间添加两个空格

时间:2019-07-13 14:48:04

标签: java android textview drawable

我正在尝试将TextView转换为Drawable并将其用作我的应用程序中的标签,但是我有一个问题,如下图所示,该文本从右边,要修复的是我在上面添加了两个空格:

当我只添加一个像textview.getText()+ " "这样的空格时,我得到了:

enter image description here

当我添加" " +textview.getText() + " "时,文本消失,我只能看到背景:

enter image description here

执行完此操作后,TextView转换为Drawable时完全不显示,这是将TextView转换为Drawable的方法

 public static Bitmap loadBitmapFromView(View view) {
        Bitmap createBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
        view.draw(canvas);
        return createBitmap;
    }


textView.setText(" " + editText.getText() + " ");
                    Bitmap bitmap = loadBitmapFromView(textView);
                    loadSticker(bitmap);

XML:

               <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:gravity="center_horizontal|center_vertical"
                android:padding="20dp"
                android:scrollbars="vertical"
                android:shadowColor="#fff"
                android:shadowDx="4"
                android:shadowDy="-4"
                android:shadowRadius="1"
                android:text="Your Text Here"
                android:textColor="#000"
                android:textSize="25sp"
                android:textStyle="bold" />

2 个答案:

答案 0 :(得分:0)

修改- 尝试使用不间断空格。

textView.setText((" " + editText.getText() + " ").replace(" ","\u00a0"));

您是否尝试过其他方法,例如

public static Bitmap loadBitmapFromView(View view) {
        Bitmap createBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        view.layout(0, 0, view.getWidth(), view.getHeight());
        view.draw(canvas);
        return createBitmap;
    }
public static Bitmap loadBitmapFromView(View view) {
        Bitmap createBitmap = Bitmap.createBitmap(view.getLayoutParams().getWidth(), view.getLayoutParams().getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        view.layout(0, 0, view.getLayoutParams().getWidth(), view.getLayoutParams().getHeight());
        view.draw(canvas);
        return createBitmap;
    }
public static Bitmap loadBitmapFromView(View view) {
        Bitmap createBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.draw(canvas);
        return createBitmap;
    }
public static Bitmap loadBitmapFromView(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap createBitmap = Bitmap.createBitmap(view.getDrawingCache());
        return createBitmap;
    }

答案 1 :(得分:0)

按照此答案将任何View转换为Bitmap Convert View to Bitmap