微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在ScrollView中包含多个段落? XML

如何解决如何在ScrollView中包含多个段落? XML

| 我试图用xml在屏幕上简单地放几个段落,因为它们太大而看不到它们,所以可以垂直滚动。我尝试了许多不同的方法,但无法弄清楚。     

解决方法

        我要补充约翰在上面说的话。您需要在scrollview内使用linearlayout来实现此目的。 类似于以下内容:
<ScrollView
    android:layout_width=\"fill_parent\"
    android:layout_height=\"fill_parent\">
    <LinearLayout
       android:layout_width=\"fill_parent\"
       android:layout_height=\"fill_parent\"
       android:orientation=\"vertical\">
       <TextView
         android:layout_width=\"fill_parent\"
         android:layout_height=\"fill_parent\"
         android:text=\"Paragraph 1\\n\\nParagraph2\\n\\nParagraph 3\"/>
   </LinearLayout>
</ScrollView>
另外,发布您到目前为止尝试过的内容将使我们为您提供更好的帮助。帮助我们帮助您!     ,        将textview嵌入线性布局中,并在顶部和底部添加一些填充。或者,也可以将文本合并为一个文本字符串,然后用
\\n
将其分开。 如果您试图将多个文本视图添加到一个滚动视图中,它将无法正常工作。一个滚动视图容器只能有一个孩子,这就是为什么您需要将文本视图放入线性布局,然后将线性布局放入滚动视图的原因。 例:
<ScrollView>
   <LinearLayout>
      <TextView text=R.string.MyText1 />
      <TextView text=R.string.MyText2 />
      <TextView text=R.string.MyText3 />
   </LinearLayout>
</ScrollView>
“ѭ3”将是您的段落,所有这些段落都将被复制并粘贴到字符串资源文件中,并使用键“ MyText1”,\“ MyText2 \”,\“ MyText3 \”等。     ,        1-将要阅读的文章放在.txt文件中 2-将其放入资产文件夹 3-将此添加到您的activity.xml
 <TextView
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Large Text\"
        android:id=\"@+id/textView2\"
        android:maxLines = \"10\"
        android:scrollbars = \"vertical\"/>
4-并将其设置为活动中的TextView
 TextView txtvue = (TextView) findViewById(R.id.textView2);
        txtvue.setMovementMethod(new ScrollingMovementMethod());
        txtvue.setText( myReader(\"your.txt\"));
5-从.txt文件中获取段落使用此功能
 public String myReader(String fileName) {
        AssetManager assetManager = getResources().getAssets();
        InputStream inputStream;
        try {
            inputStream = assetManager.open(fileName);
            int size = inputStream.available();

            byte[] buffer = new byte[size];

            inputStream.read(buffer);

            inputStream.close();

            // byte buffer into a string
            text = new String(buffer);


        } catch (IOException e) {
            e.printStackTrace();

        }

        return text;
    }
注意:请不要调用myReader(your.txt);在主视图线程中,由于阅读文字字母的过程冗长,将导致错误     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。