android-基礎編程-ScrollView

來源:互聯網
上載者:User

標籤:img   bar   break   視圖   prot   state   水平   計算   控制項   

滾動視圖(ScrollView)是指當擁有很多內容,螢幕顯示不完時,需要通過滾動來顯示完整的視圖。包括水平滾動視圖(HorizontalScrollView)和垂直滾動視圖(ScrollView)

 基本操作:

setOnTouchListener的使用:判斷ScrollView何時滑動到底部
1、getScorollY()——捲軸滑動的距離
2、getMeasuredHeight()——內容的整體高度,包括隱藏部分
3、getHeight()——顯示高度。內容未布滿螢幕,2=3;內容大於螢幕,3=螢幕高度,2>3。
4、getChildAt(int i)——擷取ScorollView的第i個子控制項

scrollTo(相對開始位置)和scrollBy(相對前一個位置):控制ScrollView視圖的位置

隱藏捲軸
1、標籤屬性:android:scrollbars="none"
2、代碼設定:
setHorizontalScrollBarEnabled(false);//隱藏橫向ScorollView
setVerticalScrollBarEnabled(false);//隱藏縱向ScorollView

注意:scrollview內容只有一個子view。

1.ScrollView使內容可以滾動。

布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="wrap_content">     <HorizontalScrollView        android:id="@+id/horizontalScrollView1"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:scrollbars="none"         >        <TextView            android:id="@+id/textView1"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:textSize="22sp"            android:text="shuiping scroll dfffffffffffffffffffffffffffffffffffffffffffffffffffffff"/>    </HorizontalScrollView>    <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="向上" />    <Button        android:id="@+id/button2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="向下" />    <ScrollView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:id="@+id/scrollview"    android:scrollbars="vertical"    android:scrollbarTrackVertical="@color/colorAccent"    android:scrollbarThumbVertical="@color/colorPrimary"        android:scrollbarStyle="outsideInset">    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical">        <TextView            android:layout_width="5000dp"            android:layout_height="500dp"            android:text="zcxzcxzcx"/>        <TextView            android:layout_width="5000dp"            android:layout_height="500dp"            android:text="zcxzcxzcx"/>    </LinearLayout></ScrollView></LinearLayout>

 

2.代碼 響應函數如下:

public class ScrollViewActi extends Activity implements View.OnClickListener{    private ScrollView scrollView;    private Button button1;    private Button button2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.scrollview);        button1 = (Button) findViewById(R.id.button1);        button2 = (Button) findViewById(R.id.button2);        scrollView = (ScrollView) findViewById(R.id.scrollview);        button1.setOnClickListener(this);        button2.setOnClickListener(this);        //這裡是為textView賦值,內容在R.string.text中,測試時最好內容長一些,這裡不再貼出。        //textView.setText(getResources().getString(R.string.text));        scrollView.setOnTouchListener(new View.OnTouchListener() {            @TargetApi(Build.VERSION_CODES.HONEYCOMB)            @Override            public boolean onTouch(View v, MotionEvent event) {                // TODO Auto-generated method stub                switch (event.getAction()) {                    //手指抬起                    case MotionEvent.ACTION_UP:                        break;                    //手指落下                    case MotionEvent.ACTION_DOWN:                        break;                    //手指滑動                    case MotionEvent.ACTION_MOVE:                        /**                         * 1、getScorollY()——捲軸滑動的距離                         * 2、getMeasuredHeight()——內容的整體高度,包括隱藏部分                         * 3、getHeight()——顯示高度。內容未布滿螢幕,2=3;內容大於螢幕,3=螢幕高度,2>3。                         */                        //頂部狀態                        if(scrollView.getScrollY()<=0){                            Log.e(">>>>>>>>>>>>>>", "頂部");                            Toast.makeText(getApplicationContext(), "頂部", Toast.LENGTH_SHORT).show();                        }                        //頂部狀態                        //TextView的總高度<=一螢幕的高度+捲軸的滾動距離(getChildAt(0):第0個子控制項)                        if(scrollView.getChildAt(0).getMeasuredHeight()<= scrollView.getScrollY() + scrollView.getHeight()){                            Log.e(">>>>>>>>>>>>>>", "底部");                            Toast.makeText(getApplicationContext(), "底部", Toast.LENGTH_SHORT).show();                            //在文本中追加內容                            //textView.append("111111111111111111111");                        }                        break;                }                return false;            }        });    }    @Override    public void onClick(View v) {        //scrollTo:以滾動視圖起始位置開始計算的        //scrollBy:相對前一次的位置,滾動相應的距離        switch (v.getId()) {            case R.id.button1:          scrollView.scrollTo(0, -30);            //    scrollView.scrollBy(0, -30);                break;            case R.id.button2:          scrollView.scrollTo(0, -30);           //     scrollView.scrollBy(0, 30);                break;        }    }}

3.效果

android-基礎編程-ScrollView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.