猿问

为什么视图没有更新?

我正在构建一个具有 2 个片段容器并具有屏幕截图功能的应用程序。问题是每当 2 个片段容器中的一个用另一个片段(已替换)更新并且我正在截屏时,它会显示以前的片段而不是更新的片段。

getView().getRootView()在活动的另一个片段中使用来截取屏幕截图。关于为什么会发生的任何建议?


拉风的咖菲猫
浏览 88回答 2
2回答

神不在的星期二

请详细说明问题或尝试添加代码。如果您不使用 FrameLayout,请使用它。添加 FrameLayout 的代码:<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; tools:context=".MainActivity">&nbsp; &nbsp; <FrameLayout&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/your_placeholder"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent"></FrameLayout></android.support.constraint.ConstraintLayout>要添加或替换新片段,请使用 FragmentTransaction。// Code inside the onCreate methodgetSupportFragmentManager().beginTransaction()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.replace(R.id.your_placeholder, new TheOtherFragment())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.commit();拍摄屏幕截图:View view = findViewById(R.id.your_placeholder);view.setDrawingCacheEnabled(true);view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache());view.setDrawingCacheEnabled(false);

犯罪嫌疑人X

拍摄屏幕截图:View view = findViewById(R.id.id_of_constraintLayout);&nbsp;// Here you assign the view Variable to the id of the View hosting all your Views.&nbsp;view.setDrawingCacheEnabled(true);view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache());view.setDrawingCacheEnabled(false);
随时随地看视频慕课网APP

相关分类

Java
我要回答