1. 程式人生 > >Android 橫向滾動和縱向滾動的TextView

Android 橫向滾動和縱向滾動的TextView

橫向滾動TextView

TextView的橫向滾動,本身自帶的跑馬燈效果+自定義一個簡單的TextView即可實現.
自定義view程式碼如下:

public class HorizontalTextview extends TextView {
    public HorizontalTextview(Context context) {
        super(context);
    }

    public HorizontalTextview(Context context, @Nullable AttributeSet attrs) {
        super
(context, attrs); } public HorizontalTextview(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean isFocused() {//返回 true 始終有焦點 return true; // return super.isFocused(); } @Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { if (focused) {//當有焦點的時候 開啟動畫 沒有的時候 什麼都不做保持狀態 super.onFocusChanged(focused, direction, previouslyFocusedRect); } } }

然後在佈局檔案引入自定義的textview,新增這幾個屬性
android:focusable=”true”
android:focusableInTouchMode=”true”
android:ellipsize=”marquee”
android:marqueeRepeatLimit=”marquee_forever”

佈局檔案:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.mytest.MainActivity">


        <com.mytest.view.HorizontalTextview
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_gravity="center_horizontal"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:id="@+id/tv1"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="春眠不覺曉,處處蚊子咬" />

</LinearLayout> 

縱向滾動TextView

縱向滾動其實實現起來也沒啥難度,ViewFlipper就可以很好的解決這個問題
佈局檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.mytest.MainActivity">


        <com.mytest.view.HorizontalTextview
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_gravity="center_horizontal"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:id="@+id/tv1"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="春眠不覺曉,處處蚊子咬" />

    <ViewFlipper
        android:layout_gravity="center_horizontal"
        android:id="@+id/myVf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoStart="true"
        android:flipInterval="2000"
        android:inAnimation="@anim/in_bottomtop"
        android:outAnimation="@anim/out_bottomtop"/>

</LinearLayout>

然後在MainActivity裡面的程式碼:

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> titleList;
    private ViewFlipper myVf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_notice = (FrameLayout) findViewById(R.id.fl_notice);
        myVf = (ViewFlipper) findViewById(R.id.myVf);
        titleList = new ArrayList();
        titleList.add("白日依山盡");
        titleList.add("黃河入海流");
        titleList.add("欲窮千里目");
        titleList.add("更上一層樓");
//        initRollNotice();


        for (int i = 0; i < titleList.size(); i++) {
            TextView textView = new TextView(this);
//            View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.view_notice_item, null);
//            TextView textView = (TextView) view.findViewById(R.id.notice_tv);
//            textView.setText(titleList.get(i));
            textView.setText(titleList.get(i));
            myVf.addView(textView);
        }
    }

}

橫向和縱向的滾動都比較好實現,這裡主要是記錄下ViewFlipper
常用的方法;

setInAnimation      設定View進入螢幕時候使用的動畫
setOutAnimation     設定View退出螢幕時候使用的動畫
showPrevious        顯示ViewFlipper裡面的上一個View
showNext            顯示ViewFlipper裡面的下一個View
setFlipInterval     設定View之間切換的時間間隔
startFlipping       使用setFlipInterval方法設定的時間間隔來開始切換所有的View,切換會迴圈進行
stopFlipping        停止View切換
isFlipping          用來判斷View切換是否正在進行
setDisplayedChild   切換到指定子View

常用的屬性:

android:autoStart:   設定自動載入下一個View

android:flipInterval:設定View之間切換的時間間隔

android:inAnimation: 設定切換View的進入動畫

android:outAnimation:設定切換View的退出動畫

相關推薦

Android 橫向滾動縱向滾動TextView

橫向滾動TextView TextView的橫向滾動,本身自帶的跑馬燈效果+自定義一個簡單的TextView即可實現. 自定義view程式碼如下: public class HorizontalTextview extends TextView {

Android中GridView水平滾動垂直滾動的實現(動態)

經過本人實驗,完美實現水平滾動和垂直滾動。話不多說,先看佈局檔案: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com

Android 中listview scrollview 滾動衝突事件的解決方法

如果一個佈局中同事引用到了listview 和scrollview 兩種滾動控制元件,那麼listview 的滾動效果將被遮蔽掉,那麼此時應加上dispatchTouchEvent()方法,然後選擇先執行滾動的控制元件加上次方法。 程式碼展示如下: package co

android初學------設定ScrollView滾動禁止滾動

哎。。。找了好久,試了好久終於弄出來了、傳說重寫OnTouchEvent事件 讓他返回false可以讓ScrollView不滾動 看程式碼 public class MyScrollView exte

簡訊轟炸之橫向轟炸縱向轟炸

白帽子您好,橫向轟炸不收,故此忽略,感謝提交。如有更多疑問可諮詢平臺客服小姐姐(QQ:3459476393),非常感謝對漏洞盒子與網際網路安全的支援,對此給您帶來的不便我們深感抱歉。 收到上面一條資訊, 才知道簡訊轟炸也分橫向轟炸和縱向轟炸。 從字面上猜猜,大概如下意思。 就我遇到的,目前可被橫向轟炸的簡

iOS UIScrollView無限迴圈滾動手動滾動

專案中的引導頁或者主頁經常會讓展示一個滾動的檢視,這種簡單的實現大家應該都會,就是一個UIScrollView加上一個UIPageControl來實現,但是當你的經理會提一些新的需求,要求你實現自動滾動並且也能手動滾動的時候,你就需要自定義來實現了。 實現原理

提高系統性能資料庫設計的橫向分割縱向分割技術

本文介紹一些關於提高系統性能方面的知識,列分割,行分割,例項分割,物理儲存分割等技術。 這篇主要講解一下資料庫的設計,因為一個好的資料結構,對整體系統的運作太重要了,請看看本文的內容。 提到程式效能,大家都知道時間複雜度的公式O(f(n))。在提高效能的這個迷局中,很多人都會

轉mysql橫向擴充套件縱向擴充套件

Scale-up(縱向擴充套件)和Scale-out(橫向擴充套件)的解釋   談到系統的可伸縮性,Scale-up(縱向擴充套件)和Scale-out(橫向擴充套件)是兩個常見的術語,對於初學者來說,很容易搞迷糊這兩個概念,這裡總結了一些把概念解釋的比較清楚的內容。

UIScrollView實現迴圈滾動自動滾動

RootViewController.m @interface RootViewController ()<UIScrollViewDelegate> @property (nonatomic, retain) UIPageControl        

RecyclerView 實現縱向橫向瀑布流 的滾動佈局

實現縱向滾動 1,在app/build.gradle檔案,dependencies中新增如下內容 ----------------- compile ‘com.android.support:recyclerview-v7:24.2.1’ --------

bootstrap滾動監聽外掛Scrollspy橫向縱向例項對比

bootstrap滾動監聽(Scrollspy)外掛,即自動更新導航外掛,會根據滾動條的位置自動更新對應的導航目標。1.如何建立滾動監聽首先Body的position需要設定為relative.<body data-spy="scroll" data-target=".

Android:TextView的垂直滾動效果,上下滾動效果

佈局裡面就是兩個自定義的TextView,上面的左右滑動的是AutoHorizontalScrollTextView; 下面上下滾動的是AutoVerticalScrollTextView; 上面左右滑動的非常好實現,直接把AutoHorizontalScrollTe

RecyclerView實現縱向滾動橫向滾動

為方便自己以後學習,自己記錄學習,大家也可以參考,有什麼問題一起探討。 今天學習RecyclerView,下邊來說一下實現資料垂直滾動和資料橫向滾動。先上圖為敬:    所用工具:Android Studio 縱向滾動 1、新增依賴庫: 開啟app/bui

Android中GalleryImageSwitcher同步自動(滾動)播放圖片庫

目標 art trac repl otto fin instance img com 本文主要內容是如何讓Gallery和ImageSwitcher控件能夠同步自動播放圖片集 ,看起來較難,然而,實現的方法非常簡單, 請跟我慢慢來。總的來說,本文要實現的效果如下圖:(截

橫向iscroll阻止縱向瀏覽器滾動

在初始化iscroll時設定: var myScroll= new IScroll('.container', { // mouseWheel : true, //滑鼠滾輪支援 // scrollbars: false, //滾動條支援 scrollY: false

Android文字垂直滾動縱向走馬燈的幾種實現方式

方法一、使用系統控制元件ViewFlipper方式: 佈局檔案: <ViewFlipper android:id="@+id/view_flipper" android:layout_width="300dp

微信小程式 scroll-view 實現橫向縱向滾動

1.縱向滾動 api文件有註明,<scroll-view scroll-y style="height: 400rpx;">   1231231231</scroll-view> 即 如果是縱向滾動,必須指定滾動區域的高度 2.橫向滾動 核心思

Android跑馬燈verticaltextview上下滾動textview實現

最近專案加入春節主題切換,然後還加入了一個公告(豎直方向滾動),找到了下面幾個方案: 1.https://github.com/sunfusheng/MarqueeView 2.textswitcher參考:https://blog.csdn.net/g777520/article/de

bootstrap table 表頭固定 、凍結列、橫向縱向滾動

前提:引入對應的js,css<link rel="stylesheet" type="text/css" href="/kaoqin/js/bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" ty

android 自定義View(二) 自定義屬性滾動的View

自定義View的屬性   在上一章中講了那麼多,這一章開始就進行實戰了。首先來一發自定義View屬性的demo。 自定義View屬性的步驟分為以下3步。 (1) 新建一個attrs.xml檔案,在這個資原始檔中定義我們需要的屬性。 (2)新建一個自定義的