1

我正在寻找一种使 TextView 可滚动的方法。我找到了这个问题的答案,涉及使用特定的 XML 属性......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:orientation="vertical">
    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
        android:id="@+id/textview" android:text="The long text, which no way have chance to fit within screen's width"
        android:maxLines="1"
        android:scrollHorizontally="true" />
</LinearLayout>

...和一小段源代码...

package spk.sketchbook;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.widget.TextView;

public class SketchbookMain extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

...并且一切都像魅力一样起作用,但只有在尝试将 TextView 中的文本向右对齐之前...

android:gravity="right|center_vertical" 

...观察,直到用户尝试滚动文本,它是不可见的(可能滚动出视图)。

不幸的是,这正是我需要做的(使右对齐的 TextView 可滚动)。

请问有什么想法吗?

4

1 回答 1

1

这是一种不同的方法:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:id="@+id/textview" 
        android:gravity="right|center_vertical"  
        android:text="The long text, which no way have chance to fit within screen's width"
        android:maxLines="1"/>
</HorizontalScrollView>

我认为你不需要任何代码来完成这项工作

于 2011-06-06T19:06:04.583 回答