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

时间:2013-01-15 10:43:27

标签: 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>

有什么建议吗?

由于

6 个答案:

答案 0 :(得分:22)

您可能正在寻找一个典型的“关于屏幕”,其中包含可以使用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>

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

<string name="about_text">
  <![CDATA[ 
    Author: Your name<br/><br/>Your description text, changelog, licences etc... ]]>
</string>

最后,使用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);
}

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

答案 1 :(得分:1)

没有标准化的方式来显示大量文字。这取决于你想要它的样子。但是很多开发人员会像你一样使用scrollview。如果您只显示文本,则应使用textview而不是编辑文本。

还有其他方法可以显示大量文字,例如viewpager

答案 2 :(得分:1)

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

     <ScrollView 
        android:id="@+id/ScrollView01"
      android:layout_height="150px" 
      android:layout_width="fill_parent">

       <TextView 
       android:id="@+id/TEXT_VIEW" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" />
     </ScrollView>
    <EditText
        android:id="@+id/EDIT_TEXT"
        android:layout_height="70px"
        android:layout_width="fill_parent"
        android:layout_alignParentBottom="true"
        android:maxHeight="70px"
    />

</RelativeLayout>   

答案 3 :(得分:0)

当只有一个子视图时,

LinearLayouts没有用处。当我想展示大量文本时,我使用了这个XML:

<!-- Display the record details in a scrolling text view. -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/[my drawable]"
    android:fillViewport="true"
    android:scrollbars="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/defaultDetail" />

</ScrollView>

答案 4 :(得分:0)

你所做的是完美的。无需更改。但是,如果只有一个ScrollView作为其子项,则最外层的LinearLayout没有意义。除此之外,一切都是应该如何完成的。

答案 5 :(得分:0)

使用此

 <TextView
   android:id="@+id/text_view"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:singleLine="false"
   android:scrollbars="vertical"
   android:textColor="#000"
   >
  </TextView>


 TextView textView = (TextView)findViewById(R.id.text_view);
 textView.setMovementMethod(ScrollingMovementMethod.getInstance());

这是自动滚动文本视图。