You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从SQLite读取的产品详情TextView在LinearLayout中被挤出屏幕如何解决

该问题和SQLite无关,是LinearLayout布局的高度分配规则导致的,修复方案如下:

问题原因

垂直方向的LinearLayout会按照子控件的声明顺序依次分配高度,你在最后一个产品详情TextView中设置了android:layout_height="match_parent",没有配置权重属性,前面4个固定高度的TextView占完屏幕空间后,剩余空间不足就会导致详情文本被挤出屏幕。

方案1:仅让详情区域可滚动(推荐,固定头部信息不滑动)

  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" />
  1. 在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 14:54:03