在android视图中显示大文本数据的最佳实践?

Al *_*aba 11 android

在我的应用程序的信息区域中,我想显示我的应用程序的简要说明.为此,我需要创建一个包含大量文本的视图.最佳做法是什么?实际上我用Scrollview创建了一个线性布局,在这里我将添加我的文本,其中还应包含一些变量值,例如我的应用版本.但我不知道是否有更好的方法可以做到这一点?

这是我目前的做法:

 <LinearLayout
     android:id="@+id/information_content"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_above="@id/bottom_footer"
     android:layout_below="@id/top_header"
     android:layout_marginLeft="@dimen/marginLeft"
     android:layout_alignParentRight="true"
     android:layout_weight="1"
     android:orientation="vertical" >

     <ScrollView
         android:id="@+id/scrollView1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent" 
             android:orientation="vertical"
             >

             <EditText
                 android:id="@+id/info_text"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:ems="10"
                 android:inputType="textMultiLine"
                 android:text="Hallo das ist ein test!!!
                 dsf" />

         </LinearLayout>
     </ScrollView>

 </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

有什么建议?

谢谢

sas*_*oar 23

您可能正在寻找一个典型的"关于屏幕",其中包含可以使用HTML格式化的简单可滚动文本.

首先,这是布局/layout/about_layout.xml.您不需要将视图包装在额外的LinearLayout中.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >

  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="12dp" >

    <TextView
      android:id="@+id/about_text_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

  </RelativeLayout>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

然后,将以下内容添加到您的/values/strings.xml:

<string name="about_text">
  <![CDATA[ 
    Author: Your name<br/><br/>Your description text, changelog, licences etc... ]]>
</string>
Run Code Online (Sandbox Code Playgroud)

最后,使用您的布局AboutActivity.java,显示HTML格式的字符串,并使用一些自动更新的信息(例如您的版本号)来丰富它.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.about_layout);

    String versionName = "";
    try {
        PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        versionName = pinfo.versionName;
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    TextView aboutTextView = (TextView) findViewById(R.id.about_text_view);

    aboutText = Html.fromHtml("<h1>Your App Name, Version " + versionName + "</h1>"
            + getString(R.string.about_text));
    aboutTextView.setText(aboutText);
}
Run Code Online (Sandbox Code Playgroud)

这就是我在我的应用程序中处理关于屏幕的方式.对不起,这个答案可能有点过头了,但我认为这是一个常见的模式.

  • 为了保持HTML分离,将HTML文件放入`/ assets`或`/ raw`文件夹(描述[这里](http://stackoverflow.com/a/5771369/1140682)),读取`InputStream`到一个字符串(描述[这里](http://stackoverflow.com/a/2549222/1140682))并用我的字符串替换我的解决方案中的`getString(R.string.about_text)`. (3认同)