在android textView中怎么实现文字的纵向的滚动
一个textview只能显示三行现在要显示的文字有四行,你知道怎么实现textview文字的滚动吗,是上下滚动的方式,不是横向滚动...
一个textview只能显示三行现在要显示的文字有四行,你知道怎么实现textview文字的滚动吗,是上下滚动的方式,不是横向滚动
展开
2个回答
展开全部
似乎textview没有纵向滚动属性,反正我没试出来,同楼上,建议用animation实现该效果,不过用不了那么多,2个就可以了,不知道楼主要在屏幕顶部实现还是其他位置? 刚才没事做了一个简单的例子,如果在顶部不用这么麻烦,写的特繁琐,把animation的各种属性都写上了,这样楼主可以根据不同的需要做修改.
package com.mygd;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
public class gundongActivity extends Activity implements AnimationListener{
/** Called when the activity is first created. */
int i = 0;
Handler handler = new Handler();
// LinearLayout linearLayout1, linearLayout2, linearLayout3;
// AnimationSet animationSet1, animationSet2, animationSet3;
// TranslateAnimation
// translateAnimation1,translateAnimation2,translateAnimation3;
// AlphaAnimation alphaAnimation1, alphaAnimation2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2);
LinearLayout linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3);
AnimationSet animationSet1 = new AnimationSet(true);
AnimationSet animationSet2 = new AnimationSet(true);
AnimationSet animationSet3 = new AnimationSet(true);
TranslateAnimation translateAnimation1 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -1);
TranslateAnimation translateAnimation2 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -0.5f);
TranslateAnimation translateAnimation3 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -1);
AlphaAnimation alphaAnimation1 = new AlphaAnimation(1, 0);
AlphaAnimation alphaAnimation2 = new AlphaAnimation(0, 1);
animationSet1.addAnimation(translateAnimation1);
animationSet1.addAnimation(alphaAnimation1);
animationSet1.setDuration(1500);
animationSet3.addAnimation(alphaAnimation2);
animationSet3.addAnimation(translateAnimation3);
animationSet3.setDuration(1500);
animationSet2.addAnimation(translateAnimation2);
animationSet2.setDuration(1500);
linearLayout1.startAnimation(animationSet1);
linearLayout2.startAnimation(animationSet2);
linearLayout3.startAnimation(animationSet3);
animationSet1.setFillAfter(true);
translateAnimation1.setRepeatCount(10);
translateAnimation2.setRepeatCount(10);
translateAnimation3.setRepeatCount(10);
alphaAnimation1.setRepeatCount(10);
alphaAnimation2.setRepeatCount(10);
animationSet2.setFillAfter(true);
animationSet3.setFillAfter(true);
animationSet1.setStartOffset(1000);
animationSet2.setStartOffset(1000);
animationSet3.setStartOffset(1000);
i++;
Log.d("xxx", "xxx"+i);
onAnimationEnd(animationSet1);
onAnimationEnd(animationSet2);
onAnimationEnd(animationSet3);
}
// else if(i%2==1){
// AnimationSet animationSet4 = new AnimationSet(true);
// AnimationSet animationSet5 = new AnimationSet(true);
// AnimationSet animationSet6 = new AnimationSet(true);
// TranslateAnimation translateAnimation4 = new TranslateAnimation(
// Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
// 0, Animation.RELATIVE_TO_SELF, 1,
// Animation.RELATIVE_TO_SELF, 0);
// TranslateAnimation translateAnimation5 = new TranslateAnimation(
// Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
// 0, Animation.RELATIVE_TO_SELF, 0.5f,
// Animation.RELATIVE_TO_SELF, 0);
// TranslateAnimation translateAnimation6 = new TranslateAnimation(
// Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
// 0, Animation.RELATIVE_TO_SELF, 1,
// Animation.RELATIVE_TO_SELF, 0);
// AlphaAnimation alphaAnimation3 = new AlphaAnimation(0, 1);
// AlphaAnimation alphaAnimation4 = new AlphaAnimation(1, 0);
// animationSet4.addAnimation(translateAnimation4);
// animationSet4.addAnimation(alphaAnimation3);
// animationSet4.setDuration(1500);
// animationSet6.addAnimation(alphaAnimation4);
// animationSet6.addAnimation(translateAnimation6);
// animationSet6.setDuration(1500);
// animationSet5.addAnimation(translateAnimation5);
// animationSet5.setDuration(1500);
// linearLayout1.startAnimation(animationSet4);
// linearLayout2.startAnimation(animationSet5);
// linearLayout3.startAnimation(animationSet6);
// animationSet4.setFillAfter(true);
// animationSet5.setFillAfter(true);
// animationSet6.setFillAfter(true);
// i++;
// Log.d("xx", "xxx"+i);
// }
@Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Log.d("xx", "end");
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
Log.d("xx", "repeat");
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
Log.d("xx", "start");
}
}
下面是xml.两个button主要是为了和顶部留有距离,
<?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"
>
<Button android:layout_height="wrap_content" android:text="Button" android:layout_width="fill_parent" android:id="@+id/button1"></Button>
<Button android:layout_height="wrap_content" android:text="Button" android:layout_width="fill_parent" android:id="@+id/button2"></Button>
<LinearLayout android:orientation="vertical" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="20dp" android:layout_marginTop="50dp">
<TextView android:layout_height="wrap_content" android:text="@string/hello" android:layout_width="fill_parent" android:id="@+id/textView2"></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical">
<TextView android:text="TextView" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="fill_parent" android:id="@+id/textView1"></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout3" android:layout_height="wrap_content" android:layout_width="fill_parent">
<TextView android:layout_height="wrap_content" android:text="1234567890" android:layout_width="wrap_content" android:id="@+id/textView3"></TextView>
</LinearLayout>
</LinearLayout>
package com.mygd;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
public class gundongActivity extends Activity implements AnimationListener{
/** Called when the activity is first created. */
int i = 0;
Handler handler = new Handler();
// LinearLayout linearLayout1, linearLayout2, linearLayout3;
// AnimationSet animationSet1, animationSet2, animationSet3;
// TranslateAnimation
// translateAnimation1,translateAnimation2,translateAnimation3;
// AlphaAnimation alphaAnimation1, alphaAnimation2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2);
LinearLayout linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3);
AnimationSet animationSet1 = new AnimationSet(true);
AnimationSet animationSet2 = new AnimationSet(true);
AnimationSet animationSet3 = new AnimationSet(true);
TranslateAnimation translateAnimation1 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -1);
TranslateAnimation translateAnimation2 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -0.5f);
TranslateAnimation translateAnimation3 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -1);
AlphaAnimation alphaAnimation1 = new AlphaAnimation(1, 0);
AlphaAnimation alphaAnimation2 = new AlphaAnimation(0, 1);
animationSet1.addAnimation(translateAnimation1);
animationSet1.addAnimation(alphaAnimation1);
animationSet1.setDuration(1500);
animationSet3.addAnimation(alphaAnimation2);
animationSet3.addAnimation(translateAnimation3);
animationSet3.setDuration(1500);
animationSet2.addAnimation(translateAnimation2);
animationSet2.setDuration(1500);
linearLayout1.startAnimation(animationSet1);
linearLayout2.startAnimation(animationSet2);
linearLayout3.startAnimation(animationSet3);
animationSet1.setFillAfter(true);
translateAnimation1.setRepeatCount(10);
translateAnimation2.setRepeatCount(10);
translateAnimation3.setRepeatCount(10);
alphaAnimation1.setRepeatCount(10);
alphaAnimation2.setRepeatCount(10);
animationSet2.setFillAfter(true);
animationSet3.setFillAfter(true);
animationSet1.setStartOffset(1000);
animationSet2.setStartOffset(1000);
animationSet3.setStartOffset(1000);
i++;
Log.d("xxx", "xxx"+i);
onAnimationEnd(animationSet1);
onAnimationEnd(animationSet2);
onAnimationEnd(animationSet3);
}
// else if(i%2==1){
// AnimationSet animationSet4 = new AnimationSet(true);
// AnimationSet animationSet5 = new AnimationSet(true);
// AnimationSet animationSet6 = new AnimationSet(true);
// TranslateAnimation translateAnimation4 = new TranslateAnimation(
// Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
// 0, Animation.RELATIVE_TO_SELF, 1,
// Animation.RELATIVE_TO_SELF, 0);
// TranslateAnimation translateAnimation5 = new TranslateAnimation(
// Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
// 0, Animation.RELATIVE_TO_SELF, 0.5f,
// Animation.RELATIVE_TO_SELF, 0);
// TranslateAnimation translateAnimation6 = new TranslateAnimation(
// Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
// 0, Animation.RELATIVE_TO_SELF, 1,
// Animation.RELATIVE_TO_SELF, 0);
// AlphaAnimation alphaAnimation3 = new AlphaAnimation(0, 1);
// AlphaAnimation alphaAnimation4 = new AlphaAnimation(1, 0);
// animationSet4.addAnimation(translateAnimation4);
// animationSet4.addAnimation(alphaAnimation3);
// animationSet4.setDuration(1500);
// animationSet6.addAnimation(alphaAnimation4);
// animationSet6.addAnimation(translateAnimation6);
// animationSet6.setDuration(1500);
// animationSet5.addAnimation(translateAnimation5);
// animationSet5.setDuration(1500);
// linearLayout1.startAnimation(animationSet4);
// linearLayout2.startAnimation(animationSet5);
// linearLayout3.startAnimation(animationSet6);
// animationSet4.setFillAfter(true);
// animationSet5.setFillAfter(true);
// animationSet6.setFillAfter(true);
// i++;
// Log.d("xx", "xxx"+i);
// }
@Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Log.d("xx", "end");
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
Log.d("xx", "repeat");
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
Log.d("xx", "start");
}
}
下面是xml.两个button主要是为了和顶部留有距离,
<?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"
>
<Button android:layout_height="wrap_content" android:text="Button" android:layout_width="fill_parent" android:id="@+id/button1"></Button>
<Button android:layout_height="wrap_content" android:text="Button" android:layout_width="fill_parent" android:id="@+id/button2"></Button>
<LinearLayout android:orientation="vertical" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="20dp" android:layout_marginTop="50dp">
<TextView android:layout_height="wrap_content" android:text="@string/hello" android:layout_width="fill_parent" android:id="@+id/textView2"></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical">
<TextView android:text="TextView" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="fill_parent" android:id="@+id/textView1"></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout3" android:layout_height="wrap_content" android:layout_width="fill_parent">
<TextView android:layout_height="wrap_content" android:text="1234567890" android:layout_width="wrap_content" android:id="@+id/textView3"></TextView>
</LinearLayout>
</LinearLayout>
来自:求助得到的回答
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询