简体   繁体   中英

Scrollable background in android TextView

I've got a TextView with a tiled background defined as

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/tile" 
android:tileMode="repeat" />

Works ok, but the problem is that when I scroll down to read more text, the background stays in the same place, whereas I'd like it to scroll with the text. Any ideas appreciated.

The text view is defined as:

<view xmlns:android="http://schemas.android.com/apk/res/android"
class="com.example.android.notepad.NoteEditor$LinedEditText"
android:id="@+id/note"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/app_bg_r"
android:textColor="#666666"
android:padding="5dp"
android:scrollbars="vertical"
android:fadingEdge="vertical" 
android:gravity="top"
android:textSize="22sp"
android:lineSpacingExtra ="7sp"
android:capitalize="sentences" />

The class LinedEditText does nothing apart from setting a custom font.

The text view is set in the Activity's onCreate with

setContentView(R.layout.note_editor);

There might be a way to get this to work only using the TextView, but if no one else proposes a solution I would recommend wrapping your TextView in a ViewGroup (FrameLayout, RelativeLayout, LinearLayout, etc) of some sort and then wrapping that with a ScrollView. It is probably not the best solution since it adds a lot of views, but it will probably work.

Set the background to the ViewGroup and have it wrap_content around the TextView. Let the ScrollView handle the scrolling.

You could try it without the extra ViewGroup to see if the background will work on the ScrollView, but you may run into the same issue again.

<ScrollView
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   <RelativeLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/app_bg_r" >
      <TextView
           android:layout_width="wrap_content"           
           android:layout_height="wrap_content" />
   </RelativeLayout>
</ScrollView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM