我目前有一个使用线性布局的程序,其中包含一个 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>
- First Activity (used to create arraylist that will populate Linear Layout on next activity)
- Portion of second activity I wish to make scrollable (Blue lines delineating what part of page I want scrollable, the rest I want to remain static)
我希望这会使线性布局部分可滚动,但在实际实现此代码时,我可以看到数据被切断且无法访问
最佳答案
您不需要 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/