androidtextview垂直滾動
『壹』 android TextView滾動條設置android:ellipsize="marquee" android:scrollbars="horizontal" android:marqu
1.你滾動條包含的列表項要超過頁面顯示範圍
2.mxl代碼中多所有的列表項都要包含在scroll中。(也就是scroll要把列表項括起來)
隨便給你段代碼 scroll裡面的內容不用仔細看,就是一些列表組件什麼的。注意sroll的格式就好了
<?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" android:background="#ff888888">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff888888">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#ff888888">
<TextView android:layout_width="wrap_content" android:id="@+id/Text" android:layout_height="wrap_content" android:text="@string/title" android:layout_gravity="center" android:textColor="#ffff00ff" android:textSize="30sp" android:layout_marginBottom="10px" android:layout_marginTop="30px"></TextView>
<Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:text="@string/backlight" android:id="@+id/backlight"></Button><Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:id="@+id/screen" android:text="@string/screen"></Button><Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:id="@+id/speaker" android:text="@string/speaker"></Button><Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:id="@+id/headset" android:text="@string/headset"></Button>
<Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:text="@string/vibrator" android:id="@+id/vibrator"></Button>
<Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:id="@+id/battery" android:text="@string/battery"></Button><Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:text="@string/dial" android:id="@+id/dial"></Button><Button android:gravity="left" android:layout_width="fill_parent" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:id="@+id/fingerpaint" android:text="@string/fingerpaint"></Button>
<Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:text="@string/touchpanel" android:id="@+id/touchpanel"></Button><Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:text="@string/gsensor" android:id="@+id/gsensor"></Button><Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:id="@+id/camera" android:text="@string/camera"></Button><Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:id="@+id/wifi" android:text="@string/wifi"></Button>
<Button android:id="@+id/picture" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:layout_width="fill_parent" android:text="@+string/picture"></Button><Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:id="@+id/bluetooth" android:text="@string/bluetooth"></Button><Button android:layout_width="fill_parent" android:gravity="left" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:id="@+id/keypress" android:text="@string/keypress"></Button><Button android:id="@+id/locateme" android:text="GPS" android:layout_width="fill_parent" android:textSize="25sp" android:layout_height="50px" android:layout_marginBottom="2px" android:gravity="left"></Button>
</LinearLayout>
</ScrollView>
</LinearLayout>
『貳』 TextView滾動如何實現
TextView實現文字滾動需要以下幾個要點:
1.文字長度長於可顯示範圍:android:singleLine="true"
2.設置可滾到,或顯示樣式:android:ellipsize="marquee"
3.TextView只有在獲取焦點後才會滾動顯示隱藏文字,因此需要在包中新建一個類,繼承TextView。重寫isFocused方法,這個方法默認行為是,如果TextView獲得焦點,方法返回true,失去焦點則返回false。跑馬燈效果估計也是用這個方法判斷是否獲得焦點,所以把它的返回值始終設置為true。
『叄』 Android中實現TextView垂直滾動,像跑馬燈那樣,但是垂直的
要實現垂直滾動的,你可以用scrollview這個組件,不需要什麼代碼,只需要在xml文件,將textview嵌套進scrollview進去就行了。
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
............//自己定義你的TextView組件
><TextView>
<ScrollView>
『肆』 android textview 怎麼實現文字滾動
scrollHorizontally屬性
設置當該文本框不夠顯示全部內容時是否允許水平滾動。
ScrollView控制項實現垂直滾動,裡面可以嵌套HorizontalScrollView,實現水平滾動。
注意,HorizontalScrollView一般不單獨使用,與ScrollView嵌套使用。
『伍』 在android textView中怎麼實現文字的縱向的滾動
似乎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>
『陸』 android 我在textview中設置了滾動條
layout:
<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"
>
<ScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="150dp"
android:fadingEdge="vertical"
android:scrollbars="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text=""
android:textSize="18sp" />
</ScrollView>
</RelativeLayout>
activity:
ScrollView sv = (ScrollView)this.findViewById(R.id.sv);
sv.setOnTouchListener(new OnTouchListener() {
@SuppressLint("NewApi")
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
height = event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
height = event.getY()-height;
if(height>=0){
Toast.makeText(MainActivity.this, "本次向下拉動了"+height, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "本次向上拉動了:"+Math.abs(height), Toast.LENGTH_SHORT).show();
}
break;
}
return false;
}
});
『柒』 如何實現textview同時具有水平滾動條和垂直滾動條
下面將介紹TextView實現滾動的三種方式:
1、嵌套在ScrollView或者HorizontalScrollView中
垂直滾動:
<scrollview android:layout_width="fill_parent"
android:layout_height="fill_parent" android:scrollbars="vertical">
<textview android:text="..."/>
</scrollview>
水平滾動:使用標簽<horizontalscrollview></horizontalscrollview>
2、設置ScrollingMovementMethod
代碼中添加:
TextView.setMovementMethod(new ScrollingMovementMethod());
XML中配置:
android:scrollbars="vertical"
3、使用Scroller來自定義TextView
『捌』 怎麼使得textview始終顯示垂直滾動條並且自動定位到最低端
這個我是用TetxtView的scrollbar屬性加代碼tv.setMovementMethod(ScrollingMovementMethod.getInstance());然後textview外面套個linearlayout fill parenttextview 是wrap content的
『玖』 android垂直TextView怎麼能讓它自動滾動
這是跑馬燈的效果。實現該功能步驟:
1、自定義Views,繼承自TextView。
2、重寫onDrow方法,計算每次的滾動的距離。
3、計算view的Y軸的重點,讓當前顯示的處於高亮顯示狀態。
4、定時的刷新View使其界面不斷的刷先,出現滾動的效果。
5、實現數據結構,將數據傳給view。
幾個步驟代碼可以參考下面
java">下面看看主要代碼:
1、創建一個類繼承TextView
/**
*@authorxushilin
*
*垂直滾動的TextViewWidget
*/
2、實現構造函數:
publicVerticalScrollTextView(Contextcontext){
super(context);
init();
}
publicVerticalScrollTextView(Contextcontext,AttributeSetattr){
super(context,attr);
init();
}
publicVerticalScrollTextView(Contextcontext,AttributeSetattr,inti){
super(context,attr,i);
init();
}
privatevoidinit(){
setFocusable(true);
//這里主要處理如果沒有傳入內容顯示的默認值
if(list==null){
list=newArrayList<Notice>();
Noticesen=newNotice(0,"暫時沒有通知公告");
list.add(0,sen);
}
//普通文字的字型大小,以及畫筆顏色的設置
mPaint=newPaint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(16);
mPaint.setColor(Color.BLACK);
mPaint.setTypeface(Typeface.SERIF);
//高亮文字的字型大小,以及畫筆顏色的設置
mPathPaint=newPaint();
mPathPaint.setAntiAlias(true);
mPathPaint.setColor(Color.RED);
mPathPaint.setTextSize(16);
mPathPaint.setTypeface(Typeface.SANS_SERIF);
}
3、從寫onDraw方法,並計算文字的行距,並且將將普通文字和高亮文字,在這個方法中繪制出來
protectedvoidonDraw(Canvascanvas){
super.onDraw(canvas);
canvas.drawColor(0xEFeffff);
Paintp=mPaint;
Paintp2=mPathPaint;
p.setTextAlign(Paint.Align.CENTER);
if(index==-1)
return;
p2.setTextAlign(Paint.Align.CENTER);
canvas.drawText(list.get(index).getName(),mX,middleY,p2);
floattempY=middleY;
for(inti=index-1;i>=0;i--){
tempY=tempY-DY;
if(tempY<0){
break;
}
canvas.drawText(list.get(i).getName(),mX,tempY,p);
}
tempY=middleY;
for(inti=index+1;i<list.size();i++){
tempY=tempY+DY;
if(tempY>mY){
break;
}
canvas.drawText(list.get(i).getName(),mX,tempY,p);
}
}
4、計算Y軸中值以及更新索引
protectedvoidonSizeChanged(intw,inth,intow,intoh){
super.onSizeChanged(w,h,ow,oh);
mX=w*0.5f;
mY=h;
middleY=h*0.5f;
}
privatelongupdateIndex(intindex){
if(index==-1)
return-1;
this.index=index;
returnindex;
}
5、定時更新view,並將介面暴露給客戶程序調用。
publicvoipdateUI(){
newThread(newupdateThread()).start();
}
{
longtime=1000;
inti=0;
publicvoidrun(){
while(true){
longsleeptime=updateIndex(i);
time+=sleeptime;
mHandler.post(mUpdateResults);
if(sleeptime==-1)
return;
try{
Thread.sleep(time);
i++;
if(i==getList().size())
i=0;
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}
HandlermHandler=newHandler();
RunnablemUpdateResults=newRunnable(){
publicvoidrun(){
invalidate();
}
};
6、xml布局文件中調用:
<?xmlversion="1.0"encoding="utf-8"?>
<!--.-->
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.demo.xsl.text.SampleView
android:id="@+id/sampleView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/selector"
/>
</LinearLayout>
7、java代碼中調用,傳遞數據:
packagecom.demo.xsl.text;
importjava.util.ArrayList;
importjava.util.List;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Handler;
{
SampleViewmSampleView;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSampleView=(SampleView)findViewById(R.id.sampleView1);
Listlst=newArrayList<Sentence>();
for(inti=0;i<30;i++){
if(i%2==0){
Sentencesen=newSentence(i,i+"、金球獎三甲揭曉C羅梅西哈維入圍");
lst.add(i,sen);
}else{
Sentencesen=newSentence(i,i+"、公牛欲用三大主力換魔獸?????");
lst.add(i,sen);
}
}
//給View傳遞數據
mSampleView.setList(lst);
//更新View
mSampleView.updateUI();
}
}
『拾』 android 如何讓多條數據在一個textview中垂直滾動顯示
Android中我們為了實現文本的滾動可以在ScrollView中嵌入一個TextView,其實TextView自己也可以實現多行滾動的,畢竟ScrollView必須只能有一個直接的子類布局。只要在layout中簡單設置幾個屬性就可以輕松實現
<TextView
android:id="@+id/tvCWJ"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"<!--垂直滾動條-->
android:singleLine="false"<!--實現多行-->
android:maxLines="15"<!--最多不超過15行-->
android:textColor="#FF0000"
/>
當然我們為了讓TextView動起來,還需要用到TextView的setMovementMethod方法設置一個滾動實例,代碼如下:
TextViewtvAndroid123=(TextView)findViewById(R.id.tvCWJ);
tvAndroid123.setMovementMethod(ScrollingMovementMethod.getInstance());//Android開發網提示相關的可以查看SDK中android.text.method分支了解更多