我正在显示文本的TextView,似乎太长,以适合
在一个屏幕上。我需要使我的TextView可滚动。我该怎么办
了吗?
代码如下:
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);
这是我用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>
注:
android:fillViewport="true"结合android:layout_weight="1.0"将使textview占用所有可用的空间。
当定义Scrollview时,不要指定android:layout_height="fill_parent"否则Scrollview不能工作!(这使我刚才浪费了一个小时!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());
}
});
}
每当你需要使用ScrollView作为父,你也使用滚动移动的方法与TextView。
当你对你的设备进行竖屏时,会出现一些问题。(如)整个页面是可滚动的,但滚动移动方法不能工作。
如果你仍然需要使用ScrollView作为父或滚动移动方法,那么你也可以使用下面的描述。
如果你没有任何问题,那么你使用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
你的问题会解决的
这是“如何应用滚动条到你的TextView”,只使用XML。
首先,你需要在main.xml文件中获取一个Textview控件,并将一些文本写入其中…是这样的:
<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>
就是这样……
简单。我是这样做的:
XML Side:
<?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>
Java side:
tv_log = (TextView) findViewById(R.id.tv_log);
tv_log.setMovementMethod(new ScrollingMovementMethod());
奖金:
为了让文本视图在文本填充时向下滚动,你必须添加:
android:gravity="bottom"
到TextView xml文件。当输入更多文本时,它会自动向下滚动。
当然,你需要使用append函数来添加文本,而不是set text:
tv_log.append("\n" + text);
我用它来记录日志。
我希望这对你有所帮助;)