java在framelayout中动态创建的textview没有宽度

jmo0nnb3  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(352)

我正在尝试将代码中的textview添加到framelayout。它位于framelayout的z顺序的imageview上方。最终的目标是允许从framelayout创建一个屏幕截图,显示图像和覆盖在上面的文本。我在使用用xml创建的文本视图而不是用动态代码版本创建的文本视图时,就可以做到这一点。create bitmap方法返回一个错误,抱怨textbox的宽度为0。在下面的代码中,我尝试将textview捕获为一个图像,以确定问题所在,因为framelayout中捕获的图像没有如预期的那样包含textview的内容。这样做,我能够找到宽度误差,我相信这是问题的根源。我尝试使用setwidth和layoutparams设置textview的宽度。最终的结果总是文本视图没有宽度,尽管它可以在手机上清晰地看到。我认为在动态创建和现有xml之间缺少了一些东西,这导致了0宽度。有人能给我指出正确的方向吗?
代码如下

public void applyTextToImage(View view) {
    // Do something in response to button

    //Hide the virtual keyboard

    InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

    //Get the text to overlay on the image
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();

    //Bring the overlay layout to the front
   //LinearLayout overlay_layout = (LinearLayout) findViewById(R.id.image_Overlay_Layout);
   //overlay_layout.bringToFront();

    //Apply the new text to the text box

    /* Old code to get the view that is shown in the layout
    TextView text_overlay = (TextView) findViewById(R.id.image_Overlay);
    text_overlay.bringToFront();
    text_overlay.setText(message);
    */

    //New code to create a view dynamically instead

    TextView text_Overlay = new TextView(this);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        text_Overlay.setId(Utils.generateViewId());
    }
    else
    {
        text_Overlay.setId(TextView.generateViewId()); //static class
    }
    FrameLayout image_Layout =  (FrameLayout) findViewById(R.id.image_Layout);
    //View image_Layout =  (View) findViewById(R.id.image_Layout);
    //FrameLayout.LayoutParams fParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
    FrameLayout.LayoutParams fParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
    fParams.gravity = Gravity.CENTER;

   // text_Overlay.setMaxWidth(image_Layout.getWidth());
   // text_Overlay.setWidth(image_Layout.getWidth());
    //image_Layout.addView(text_Overlay, fParams);
    image_Layout.addView(text_Overlay, fParams);
    Toast.makeText(this,"TextView Width: " + text_Overlay.getWidth(),Toast.LENGTH_LONG).show();

    //TODO: Something has forced the dynamic layout to not be saved in  the bitmap try removing the params and set the values on the textview itself
    //TODO: for some reason the width keeps coming back as 0 could be that the image_Layout is 0 too

    text_Overlay.setGravity(Gravity.CENTER);
    text_Overlay.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
    //text_Overlay.setWidth(250dp);

    text_Overlay.setTextSize(pixels);
    text_Overlay.setTextColor(Color.RED);
    text_Overlay.bringToFront();
    text_Overlay.setText(message);
    text_Overlay.setVisibility(View.VISIBLE);
    //End of new dynamic code

    if (folderCheck()){
        try {
            String filePath = getFilePath();
            int myId = text_Overlay.getId();

            Bitmap bitmap;
            View v1 = findViewById(myId);

            v1.setDrawingCacheEnabled(true);

            v1.buildDrawingCache();

            bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);
            // End imported code

            streamBitmapToFile(bitmap, filePath);
        }
        catch (Exception e)
        {
            Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG);
            Log.e("Bitmap Creation","Couldn't create bitmap error as: "  + e.toString());
        }
    }
}

xml内容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="false">
    <!--app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_my"-->

    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message"
        android:enabled="false"/>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="applyTextToImage"
        android:enabled="false"
        android:id="@+id/overlayButton"/>

</LinearLayout>

<FrameLayout
    android:id="@+id/image_Layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true">

    <ImageView
        android:id="@+id/image_View"
        android:layout_width="match_parent"
        android:layout_height="250dp" />

   <!--<TextView
        android:id="@+id/image_Overlay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAlignment="center"
        android:textSize="25dp"
        android:textColor="#ff0000"/>-->

</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">

<Button android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/open_gallery"
    android:onClick="openGallery">
</Button>

<Button android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/new_image"
    android:onClick="newImage">
</Button>

</LinearLayout>
</RelativeLayout>
xsuvu9jc

xsuvu9jc1#

卡拉库里指出了这些弱点和解决这些弱点的途径,以供参考。
为侦听器实现的额外代码如下所示。

IMAGE_CAPTURE_REQUESTED = true;

    // New Listener
    ViewTreeObserver vto = image_Layout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                image_Layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
            else
            {
                image_Layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }

            if (IMAGE_CAPTURE_REQUESTED) {
                //int myId = text_Overlay.getId();
                //Toast.makeText(MyActivity.this, "Text Overlay Width " + text_Overlay.getWidth() ,Toast.LENGTH_LONG).show();
                overlayTextAndExportImage();
            }
        }
    }); //End New Listener

重新绘制布局后,新的overlaytextandimportimage将调用所有图像创建例程。

rnmwe5a2

rnmwe5a22#

这景色还没有被测量过。在系统执行另一个布局过程之前,视图会将其宽度报告为零。
在我看来,最好将textview留在xml布局中,而只是使其不可见( android:visibility="invisible" )直到您需要它,然后使用 setVisbility(View.VISIBLE) . (请注意,如果您将其设置为 gone ,也不会进行测量。)

相关问题