我显示的案文在控似乎太长时间来适应 进入一个屏幕。我需要做我的控滚动。我怎么可以做的 那?

这里是代码:

final TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.splash);
tv.setTypeface(face);
tv.setTextSize(18);
tv.setTextColor(R.color.BROWN);
tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
tv.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent e) {
        Random r = new Random();
        int i = r.nextInt(101);
        if (e.getAction() == e.ACTION_DOWN) {
            tv.setText(tips[i]);
            tv.setBackgroundResource(R.drawable.inner);
        }
        return true;
    }
});
setContentView(tv);
有帮助吗?

解决方案

您不需要实际使用ScrollView

只需设置

android:scrollbars = "vertical"

在布局的xml文件的TextView的性能。

然后使用:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

在您的代码。

宾果,它滚动!

其他提示

这是我做到了纯粹的在XML:

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

    <ScrollView
        android:id="@+id/SCROLLER_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:fillViewport="true">

        <TextView
            android:id="@+id/TEXT_STATUS_ID"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"/>
    </ScrollView>
</LinearLayout>

注:

  1. android:fillViewport="true" 结合 android:layout_weight="1.0" 将使控采取了所有可用的空间。

  2. 当定义的滚动型的,不指定 android:layout_height="fill_parent" 否则滚动型不工作!(这引起了我要浪费一个小时就现在!FFS).

亲提示:

到编程方式滚动到底后追加的文字,使用这样的:

mTextStatus = (TextView) findViewById(R.id.TEXT_STATUS_ID);
mScrollView = (ScrollView) findViewById(R.id.SCROLLER_ID);

private void scrollToBottom()
{
    mScrollView.post(new Runnable()
    {
        public void run()
        {
            mScrollView.smoothScrollTo(0, mTextStatus.getBottom());
        }
    });
}

所有这确实是必要的是setMovementMethod()。下面是一个使用的LinearLayout一个例子。

文件 main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:id="@+id/tv1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/hello"
    />
</LinearLayout>

文件 WordExtractTest.java

public class WordExtractTest extends Activity {

    TextView tv1;

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

        tv1 = (TextView)findViewById(R.id.tv1);

        loadDoc();
    }

    private void loadDoc() {

        String s = "";

        for(int x=0; x<=100; x++) {
            s += "Line: " + String.valueOf(x) + "\n";
        }

        tv1.setMovementMethod(new ScrollingMovementMethod());

        tv1.setText(s);
    }
}

请您的TextView刚刚添加该

TextView textview= (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(new ScrollingMovementMethod());

这是没有必要把在

android:Maxlines="AN_INTEGER"`

您可以通过简单地增加你的工作:

android:scrollbars = "vertical"

和,把这段代码在你的Java类:

textview.setMovementMethod(new ScrollingMovementMethod());

你可以

  1. 环绕 TextView 通过一个 ScrollView;或
  2. 设置运动的方法 ScrollingMovementMethod.getInstance();.

我发现的最佳方式:

与这些额外的属性一个EditText替换的TextView:

android:background="@null"
android:editable="false"
android:cursorVisible="false"

有不需要在滚动型包装。

这是 “如何滚动条适用于您的TextView”,只使用XML。

首先,你需要采取一个TextView控制在的main.xml 的文件,并写入一些文本进去......这样的:

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

下一步,将文本视图控制在滚动视图之间以显示这个文本滚动条:

<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"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/long_text"/>

    </ScrollView>
</RelativeLayout>

这就是它......

简单的。这是我怎么做:

  1. XML的一面:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        tools:context="com.mbh.usbcom.MainActivity">
        <TextView
            android:id="@+id/tv_log"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:text="Log:" />
    </RelativeLayout>
    
  2. Java面:

    tv_log = (TextView) findViewById(R.id.tv_log);
    tv_log.setMovementMethod(new ScrollingMovementMethod());
    

奖励:

让的文本景下滚动案文填补它,你必须添加:

    android:gravity="bottom"

到控xml文件。它会往下滚动时自动更多的文本。

当然,你需要添加的案文使用的附加功能而不是组案文:

    tv_log.append("\n" + text);

我使用它,用于登录的目的。

我希望这可以帮助;)

“亲尖端”上方从有人的地方(制作的TextView滚动Android上)的伟大工程,但是,如果你要动态地添加文本的滚动型和想自动滚动至底部追加后的只有的当用户处于的底部滚动型? (也许是因为如果用户滚动念你不想自动恢复为底的东西追加,这将是恼人的过程。)

总之,在这里它是:

if ((mTextStatus.getMeasuredHeight() - mScrollView.getScrollY()) <=
        (mScrollView.getHeight() + mTextStatus.getLineHeight())) {
    scrollToBottom();
}

在mTextStatus.getLineHeight()将让这个你不scrollToBottom()如果用户是从滚动型的端部的一行内。

这将提供一个滚动条平滑滚动文本。

ScrollView scroller = new ScrollView(this);
TextView tv = new TextView(this);
tv.setText(R.string.my_text);
scroller.addView(tv);

如果您希望文本TextView的内滚动,那么你可以按照以下内容:

首先,你应该有继承TextView的。

和然后使用该

以下是一个子类的TextView的一个例子。

public class AutoScrollableTextView extends TextView {

    public AutoScrollableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

    public AutoScrollableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

    public AutoScrollableTextView(Context context) {
        super(context);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

现在,你必须使用的XML这种方式:

 <com.yourpackagename.AutoScrollableTextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="This is very very long text to be scrolled"
 />

就是这样。

我没有找到TextView的滚动支持“扫视”的姿态,在那里继续轻弹后,向上或向下滚动。我最终实现的是自己,因为我不想使用滚动型由于种种原因,并且似乎没有成为一个MovementMethod既让我选择文本,然后点击链接。

添加以下在XML TextView的。

android:scrollbars="vertical"

最后,添加

textView.setMovementMethod(new ScrollingMovementMethod());

Java文件英寸

当你用滚动完成,当你输入的任何视图中添加此行到视图的最后一行:

((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);

添加到您的XML布局:

android:ellipsize="marquee"
android:focusable="false"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="To Make An textView Scrollable Inside The TextView Using Marquee"

和在代码必须编写以下行:

textview.setSelected(true);
textView.setMovementMethod(new ScrollingMovementMethod());

如果你不想使用的EditText的解决方案,那么你可能有更好的运气:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout);
    (TextView)findViewById(R.id.yourTextViewId).setMovementMethod(ArrowKeyMovementMethod.getInstance());
}

下面的代码创建一个自动水平滚动的TextView:

虽然增加的TextView到XML使用

<TextView android:maxLines="1" 
          android:ellipsize="marquee"
          android:scrollHorizontally="true"/>

设置在的onCreate()的TextView的下列属性

tv.setSelected(true);
tv.setHorizontallyScrolling(true); 

使用像这样

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:maxLines = "AN_INTEGER"
    android:scrollbars = "vertical"
/>

在科特林用于使TextView的滚动

myTextView.movementMethod= ScrollingMovementMethod()

和还添加以xml此属性

    android:scrollbars = "vertical"

在我的case.Constraint Layout.AS 2.3

代码实现:

YOUR_TEXTVIEW.setMovementMethod(new ScrollingMovementMethod());

XML:

android:scrollbars="vertical"
android:scrollIndicators="right|end"

maxLinesscrollbars内部TextView的XML格式。

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:maxLines="5" // any number of max line here.
    />

如果是Java代码。

textView.setMovementMethod(new ScrollingMovementMethod());

我,当我使用TextView内部ScrollView有这个问题。该解决方案为我工作。

scrollView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                description.getParent().requestDisallowInterceptTouchEvent(false);

                return false;
            }
        });

        description.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                description.getParent().requestDisallowInterceptTouchEvent(true);

                return false;
            }
        });

每当需要的使用滚动型作为父下,而且你还使用滚动移动法的TextView。

当你的纵向到横向的那段时间发生的一些问题设备。 (像)整个页是可滚动的,但滚动移动方法不能正常工作。

如果您仍需要使用滚动型作为父母或滚动移动方法则还使用下面描述第

如果你没有任何问题,那么你使用的EditText代替的TextView

见下文:

<EditText
     android:id="@+id/description_text_question"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@null"
     android:editable="false"
     android:cursorVisible="false"
     android:maxLines="6"/>

<强>在此,的EditText行为类似的TextView

和您的问题将得到解决。

XML - 您可以使用android:scrollHorizontally属性

无论文本被允许比更宽的视图(并且因此可水平滚动)。

可能是一个布尔值,例如“真”或“假”。

Prigramacaly - setHorizontallyScrolling(boolean)

尝试这种情况:

android:scrollbars = "vertical"

我这个折腾了一个星期,终于想通了如何使这项工作!

我的问题是,一切都将滚动作为一个“块”。本身是滚动的文字,但作为一个块,而不是一行一行。这显然并没有为我工作,因为这将切断的底线。以前所有的解决方案并没有为我工作,所以我制作我自己的。

下面是迄今为止最容易的解决方案:

请称为类文件:封装内 'PerfectScrollableTextView',然后复制并在粘贴此代码:

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

public class PerfectScrollableTextView extends TextView {

    public PerfectScrollableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    public PerfectScrollableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    public PerfectScrollableTextView(Context context) {
        super(context);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

最后更改 '的TextView' 在XML:

自:<TextView

要:<com.your_app_goes_here.PerfectScrollableTextView

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top