java - 如何修复包含 Textview 的线性布局的 Scrollview

标签 java android-studio

我目前有一个使用线性布局的程序,其中包含一个 Textview,其中填充了 x 个值(取决于单独 Activity 中确定的 arrayList 大小),但 Scrollview 并未按我的预期工作。

由于 Scrollview 只能有一个直接子级,因此我使用了线性布局,其中嵌套了 TextView;但是,当我将这两个内容包装在 Scrollview 中时,线性布局仍然不可滚动

//This is what I am using to populate the Linear Layout that I'm trying to make scrollable
TextView DisplayString = (TextView)findViewById(R.id.stringNumsCon1);
        LinearLayout LinContainer = (LinearLayout)findViewById(R.id.LinLay);

        Intent intent = getIntent();
        ArrayList<String> timerVals = (ArrayList<String>)getIntent().getSerializableExtra("timerVals");

        DisplayString.setTextSize(15);
        DisplayString.setTextColor(Color.BLACK);
        for(int i=0; i<timerVals.size(); i++){
            DisplayString.append(timerVals.get(i));
            DisplayString.append("\n");
        }

//This is how I am trying to make it scrollable within the xml
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/LinLay"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="391dp">
            <TextView
                android:id="@+id/stringNumsCon1"
                android:layout_width="match_parent"
                android:layout_height="391dp"
                android:orientation="vertical">
            </TextView>

        </LinearLayout>
    </ScrollView>

我希望这会使线性布局部分可滚动,但在实际实现此代码时,我可以看到数据被切断且无法访问

最佳答案

您不需要 ScrollView,也不需要 LinearLayout。将它们从布局中删除。 您所需要的只是在 xml 中的 TextView 中设置:

android:scrollbars = "vertical"

所以你的最终 xml 布局将是:

<TextView
   android:id="@+id/stringNumsCon1"
   android:layout_width="match_parent"
   android:layout_height="391dp"
   android:orientation="vertical"
   android:scrollbars = "vertical" />

然后在 Activity/fragment 的 onCreate 中设置移动方法,如下所示:

Java

DisplayString.setMovementMethod(new ScrollingMovementMethod());

Kotlin

DisplayString.movementMethod = ScrollingMovementMethod()

P.S.1:您可以找到原始答案here
P.S.2:考虑对变量名称使用驼峰式大小写命名约定。例如。使用 displayString 而不是 DisplayString。

关于java - 如何修复包含 Textview 的线性布局的 Scrollview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57014653/

相关文章:

java - 检查 Android native 联系人上的联系人是否已更改以更新我的数据库(sqlite)

java - 在 Swing 外使用 Swing 计时器可以吗?

java - Jboss过滤器无法捕获所有客户端的HTTP请求

android-studio - Android Studio 中的 CMake 语法高亮和助手

c - 获取 DRAM 或 SRAM galaxy S7 的起始地址

android-studio - Android TV模拟器:方向错误

java - 使用正则表达式获取 2 个字符串之间的差异

java - Android Webview 加载对话框未被关闭

Android Studio - panic : Could not open AVD

android - 导入项目时发生Gradle构建错误: “Error: The file name must end with .xml”