从SQLite读取的产品详情TextView在LinearLayout中被挤出屏幕如何解决
该问题和SQLite无关,是LinearLayout布局的高度分配规则导致的,修复方案如下:
问题原因
垂直方向的LinearLayout会按照子控件的声明顺序依次分配高度,你在最后一个产品详情TextView中设置了android:layout_height="match_parent",没有配置权重属性,前面4个固定高度的TextView占完屏幕空间后,剩余空间不足就会导致详情文本被挤出屏幕。
方案1:仅让详情区域可滚动(推荐,固定头部信息不滑动)
- 修改布局文件中最后一个TextView的属性:
<!-- 修改后的详情TextView控件 --> <TextView android:id="@+id/txtViewProductDescriptionDetailedFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:hint="Product Description: " android:inputType="textMultiLine" android:scrollbars="vertical" />
- 在Fragment的
onViewCreated生命周期方法中添加滚动支持代码:
// 导入必要包 import android.text.method.ScrollingMovementMethod; // 配置TextView滚动 txtViewProductDescriptionDetailedFragment.setMovementMethod(new ScrollingMovementMethod());
方案2:整个页面可滑动(适合内容多、希望全部内容都能上下滑动查看的场景)
将根布局替换为ScrollView嵌套LinearLayout,修改后完整布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".ProductDescriptionFragment"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Product Fragment" android:textSize="50dp" /> <TextView android:id="@+id/txtViewProductNameFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Product Name: " android:inputType="text" /> <TextView android:id="@+id/txtViewProductPriceFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Product Price: " android:inputType="text" /> <TextView android:id="@+id/txtViewProductQuantityFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Product Quantity: " android:inputType="text" /> <TextView android:id="@+id/txtViewProductDescriptionDetailedFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Product Description: " android:inputType="textMultiLine" /> </LinearLayout> </ScrollView>
内容的提问来源于stack exchange,提问作者Liam
相关产品推荐
相关产品推荐

