1414import android .view .View ;
1515import android .view .View .OnClickListener ;
1616import android .view .ViewGroup ;
17+ import android .view .ViewTreeObserver ;
1718import android .view .Window ;
1819import android .view .WindowManager ;
1920import android .webkit .ConsoleMessage ;
@@ -298,6 +299,7 @@ public void onStop() {
298299
299300 @ Override
300301 protected void onDestroy () {
302+ removeTtsContainerHeightListener ();
301303 EventHelper .unregister (this );
302304
303305 super .onDestroy ();
@@ -1293,12 +1295,14 @@ public void toggleTTS(boolean autoPlay) {
12931295
12941296 getSupportFragmentManager ()
12951297 .beginTransaction ()
1296- .add (R .id .viewMain , ttsFragment , TAG_TTS_FRAGMENT )
1298+ .add (R .id .tts_container , ttsFragment , TAG_TTS_FRAGMENT )
12971299 .commit ();
12981300
12991301 settings .setTtsVisible (true );
13001302
13011303 initTtsForArticle ();
1304+
1305+ setupTtsContainerHeightListener ();
13021306 } else {
13031307 getSupportFragmentManager ()
13041308 .beginTransaction ()
@@ -1308,11 +1312,64 @@ public void toggleTTS(boolean autoPlay) {
13081312 ttsFragment = null ;
13091313
13101314 settings .setTtsVisible (false );
1315+
1316+ adjustScrollViewPaddingForTts (0 );
1317+
1318+ removeTtsContainerHeightListener ();
13111319 }
13121320
13131321 invalidateOptionsMenu ();
13141322 }
13151323
1324+ private ViewTreeObserver .OnGlobalLayoutListener ttsContainerLayoutListener ;
1325+
1326+ private void setupTtsContainerHeightListener () {
1327+ FrameLayout ttsContainer = findViewById (R .id .tts_container );
1328+ if (ttsContainer == null ) return ;
1329+
1330+ removeTtsContainerHeightListener ();
1331+
1332+ ttsContainerLayoutListener = new ViewTreeObserver .OnGlobalLayoutListener () {
1333+ private int lastHeight = -1 ;
1334+
1335+ @ Override
1336+ public void onGlobalLayout () {
1337+ FrameLayout container = findViewById (R .id .tts_container );
1338+ if (container != null ) {
1339+ int currentHeight = container .getHeight ();
1340+ if (currentHeight != lastHeight && currentHeight > 0 ) {
1341+ lastHeight = currentHeight ;
1342+ adjustScrollViewPaddingForTts (currentHeight );
1343+ }
1344+ }
1345+ }
1346+ };
1347+
1348+ ttsContainer .getViewTreeObserver ().addOnGlobalLayoutListener (ttsContainerLayoutListener );
1349+ }
1350+
1351+ private void removeTtsContainerHeightListener () {
1352+ if (ttsContainerLayoutListener != null ) {
1353+ FrameLayout ttsContainer = findViewById (R .id .tts_container );
1354+ if (ttsContainer != null ) {
1355+ ttsContainer .getViewTreeObserver ().removeOnGlobalLayoutListener (ttsContainerLayoutListener );
1356+ ttsContainer .getViewTreeObserver ().removeOnGlobalLayoutListener (ttsContainerLayoutListener );
1357+ }
1358+ ttsContainerLayoutListener = null ;
1359+ }
1360+ }
1361+
1362+ private void adjustScrollViewPaddingForTts (int ttsHeight ) {
1363+ if (scrollView != null ) {
1364+ scrollView .setPadding (
1365+ scrollView .getPaddingLeft (),
1366+ scrollView .getPaddingTop (),
1367+ scrollView .getPaddingRight (),
1368+ ttsHeight
1369+ );
1370+ }
1371+ }
1372+
13161373 private void initTtsForArticle () {
13171374 if (ttsFragment != null ) {
13181375 ttsFragment .initForArticle (article );
0 commit comments