TextView选中弹出菜单时下方文本被截断的修复咨询
问题修复:TextView选中文本时下方控件被截断
问题描述
给TextView添加textIsSelectable="true"开启文本选中功能后,选中文本弹出操作菜单时,下方的TextView会被截断遮挡。未选中状态下所有控件正常显示,选中后操作菜单占用屏幕空间,导致原有布局被挤压。
修复方案
方案1:适配系统窗口空间
给最外层LinearLayout添加android:fitsSystemWindows="true"属性,让布局自动预留系统UI(包括文本选中后的操作栏)的空间,避免挤压下方控件:
修改后的布局代码:
<LinearLayout 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:background="@color/light_yellow" android:gravity="center_horizontal" android:orientation="vertical" android:fitsSystemWindows="true" <!-- 添加适配属性 --> tools:context=".MainActivity"> <TextView android:id="@+id/selected_date_field" style="@style/MainTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="@string/selected_date_field" /> <TextView android:id="@+id/selected_date" style="@style/SecondTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/selected_date" /> <TextView android:id="@+id/in_minutes_field" style="@style/MainTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textIsSelectable="true" android:textColorHighlight="@color/blue" android:text="@string/in_minutes_field" /> <TextView android:id="@+id/in_minutes_till_date" style="@style/SecondTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/in_minutes_till_date" /> </LinearLayout>
方案2:用ScrollView包裹布局
如果方案1无效,将外层LinearLayout嵌套进ScrollView,让布局支持滚动。即使操作栏占用空间,用户也能滑动查看被遮挡的控件:
<ScrollView 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:background="@color/light_yellow" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/selected_date_field" style="@style/MainTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="@string/selected_date_field" /> <TextView android:id="@+id/selected_date" style="@style/SecondTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/selected_date" /> <TextView android:id="@+id/in_minutes_field" style="@style/MainTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textIsSelectable="true" android:textColorHighlight="@color/blue" android:text="@string/in_minutes_field" /> <TextView android:id="@+id/in_minutes_till_date" style="@style/SecondTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/in_minutes_till_date" /> </LinearLayout> </ScrollView>
方案3:调整布局高度策略
将外层LinearLayout的android:layout_height改为wrap_content,避免固定高度导致的布局挤压,此方案结合ScrollView使用效果最佳。
内容的提问来源于stack exchange,提问作者Anstasia Kotova
相关产品推荐
相关产品推荐

