Best practice to show big text data in an android view?

前端 未结 6 1372
渐次进展
渐次进展 2020-12-28 09:38

In my information area of my app I want to show a brief description of my app. For that I need to create a view with a lot of text. What is the best practice for that ? Actu

相关标签:
6条回答
  • 2020-12-28 10:00

    use this

     <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());
    

    this is auto scroll textview.

    0 讨论(0)
  • 2020-12-28 10:05
    <?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>   
    
    0 讨论(0)
  • 2020-12-28 10:08

    There is no standardised way to display a lot of text. It depends on how you want it to look. However a lot of developers will use the scrollview like you have. Tho if you are only displaying text you should use a textview rather than the edittext you have.

    There are other way to display lots of text such as a viewpager

    0 讨论(0)
  • 2020-12-28 10:11

    You are probably looking for a typical "About Screen" with simple scrollable text that may be formatted using HTML.

    Firstly, here is the layout /layout/about_layout.xml. You don't need to wrap your view in an extra 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>
    

    Afterwards, add the following to your /values/strings.xml:

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

    Finally, use the layout in your AboutActivity.java, display the string HTML-formatted and enrich it with some auto-updated information, such as your version number.

    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);
    }
    

    That's how I deal with the About Screen in my app. Sorry for this answer might be a bit over the top, but I think it's a commonly requested pattern.

    0 讨论(0)
  • 2020-12-28 10:11

    What you're doing is perfect. There are no changes required. But your outermost LinearLayout doesn't make sense if that has only one ScrollView as its child. Except for that, everything is exactly how it should be done.

    0 讨论(0)
  • 2020-12-28 10:15

    LinearLayouts serve no purpose when there is only one child View. When I wanted to show a lot of text, I used this 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>
    
    0 讨论(0)
提交回复
热议问题