滚动TextView重绘问题

时间:2011-06-06 17:31:36

标签: android textview scrollable right-align

我正在寻找一种使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可滚动)。

有什么想法吗?

1 个答案:

答案 0 :(得分: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>

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

相关问题