日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android学习笔记:ScrollView卷轴视图

發(fā)布時間:2025/4/5 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android学习笔记:ScrollView卷轴视图 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
ScrollView卷軸視圖是指當(dāng)擁有很多內(nèi)容,一屏顯示不完時,需要通過滾動跳來顯示的視圖.的使用:

Java代碼
  • <?xml?version="1.0"?encoding="utf-8"?> ??
  • <ScrollView?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:id="@+id/ScrollView"?android:layout_width="fill_parent"??
  • ????android:layout_height="wrap_content"?android:scrollbars="vertical"> ??
  • ????<LinearLayout?android:id="@+id/LinearLayout"??
  • ????????android:orientation="vertical"?android:layout_width="fill_parent"??
  • ????????android:layout_height="wrap_content"> ??
  • ????????<TextView?android:id="@+id/TestView"?android:layout_width="fill_parent"??
  • ????????????android:layout_height="wrap_content"?android:text="TestView0"?/> ??
  • ????????<Button?android:id="@+id/Button"?android:text="Button0"?android:layout_width="fill_parent"??
  • ????????????android:layout_height="wrap_content"></Button> ??
  • ????</LinearLayout> ??
  • </ScrollView>??
  • <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/ScrollView" android:layout_width="fill_parent"android:layout_height="wrap_content" android:scrollbars="vertical"><LinearLayout android:id="@+id/LinearLayout"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="wrap_content"><TextView android:id="@+id/TestView" android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="TestView0" /><Button android:id="@+id/Button" android:text="Button0" android:layout_width="fill_parent"android:layout_height="wrap_content"></Button></LinearLayout> </ScrollView>

    Java代碼
  • package?com.Aina.Android; ??
  • ??
  • import?android.app.Activity; ??
  • import?android.os.Bundle; ??
  • import?android.os.Handler; ??
  • import?android.view.KeyEvent; ??
  • import?android.view.View; ??
  • import?android.widget.Button; ??
  • import?android.widget.LinearLayout; ??
  • import?android.widget.ScrollView; ??
  • import?android.widget.TextView; ??
  • ??
  • public?class?Test_ScrollView?extends?Activity?{ ??
  • ????/**?Called?when?the?activity?is?first?created.?*/??
  • ????private?LinearLayout?mLayout; ??
  • ????private?ScrollView?sView; ??
  • ????private?final?Handler?mHandler?=?new?Handler(); ??
  • ??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{ ??
  • ????????super.onCreate(savedInstanceState); ??
  • ????????setContentView(R.layout.main); ??
  • ????????//?創(chuàng)建一個線性布局??
  • ????????mLayout?=?(LinearLayout)?this.findViewById(R.id.LinearLayout); ??
  • ????????//?創(chuàng)建一個ScrollView對象??
  • ????????sView?=?(ScrollView)?this.findViewById(R.id.ScrollView); ??
  • ????????Button?mBtn?=?(Button)?this.findViewById(R.id.Button); ??
  • ????????mBtn.setOnClickListener(mClickListener);//?添加點擊事件監(jiān)聽??
  • ????} ??
  • ??
  • ????public?boolean?onKeyDown(int?keyCode,?KeyEvent?event){ ??
  • ????????Button?b?=?(Button)?this.getCurrentFocus(); ??
  • ????????int?count?=?mLayout.getChildCount(); ??
  • ????????Button?bm?=?(Button)?mLayout.getChildAt(count-1); ??
  • ??
  • ????????if(keyCode==KeyEvent.KEYCODE_DPAD_UP?&&?b.getId()==R.id.Button){ ??
  • ????????????bm.requestFocus(); ??
  • ????????????return?true; ??
  • ????????}else?if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN?&&?b.getId()==bm.getId()){ ??
  • ????????????this.findViewById(R.id.Button).requestFocus(); ??
  • ????????????return?true; ??
  • ????????} ??
  • ????????return?false; ??
  • ????} ??
  • ????//?Button事件監(jiān)聽,當(dāng)點擊第一個按鈕時增加一個button和一個textview??
  • ????private?Button.OnClickListener?mClickListener?=?new?Button.OnClickListener()?{ ??
  • ??
  • ????????private?int?index?=?1; ??
  • ??
  • ????????@Override??
  • ????????public?void?onClick(View?v)?{ ??
  • ????????????TextView?tView?=?new?TextView(Test_ScrollView.this);//定義一個TextView??
  • ????????????tView.setText("TextView"?+?index);//設(shè)置TextView的文本信息??
  • ????????????//設(shè)置線性布局的屬性??
  • ????????????LinearLayout.LayoutParams?params?=?new?LinearLayout.LayoutParams( ??
  • ????????????????????LinearLayout.LayoutParams.FILL_PARENT, ??
  • ????????????????????LinearLayout.LayoutParams.WRAP_CONTENT); ??
  • ????????????mLayout.addView(tView,?params);//添加一個TextView控件??
  • ????????????Button?button?=?new?Button(Test_ScrollView.this);//定義一個Button??
  • ????????????button.setText("Button"?+?index);//設(shè)置Button的文本信息??
  • ????????????button.setId(index++); ??
  • ????????????mLayout.addView(button,?params);//添加一個Button控件??
  • ????????????mHandler.post(mScrollToButton);//傳遞一個消息進行滾動??
  • ????????} ??
  • ??
  • ????}; ??
  • ????private?Runnable?mScrollToButton?=?new?Runnable()?{ ??
  • ??
  • ????????@Override??
  • ????????public?void?run()?{ ??
  • ????????????int?off?=?mLayout.getMeasuredHeight()?-?sView.getHeight(); ??
  • ????????????if?(off?>?0)?{ ??
  • ????????????????sView.scrollTo(0,?off);//改變滾動條的位置??
  • ????????????} ??
  • ????????} ??
  • ??
  • ????}; ??
  • ??
  • ??
  • }??
  • package com.Aina.Android;import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView;public class Test_ScrollView extends Activity {/** Called when the activity is first created. */private LinearLayout mLayout;private ScrollView sView;private final Handler mHandler = new Handler();@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 創(chuàng)建一個線性布局mLayout = (LinearLayout) this.findViewById(R.id.LinearLayout);// 創(chuàng)建一個ScrollView對象sView = (ScrollView) this.findViewById(R.id.ScrollView);Button mBtn = (Button) this.findViewById(R.id.Button);mBtn.setOnClickListener(mClickListener);// 添加點擊事件監(jiān)聽}public boolean onKeyDown(int keyCode, KeyEvent event){Button b = (Button) this.getCurrentFocus();int count = mLayout.getChildCount();Button bm = (Button) mLayout.getChildAt(count-1);if(keyCode==KeyEvent.KEYCODE_DPAD_UP && b.getId()==R.id.Button){bm.requestFocus();return true;}else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN && b.getId()==bm.getId()){this.findViewById(R.id.Button).requestFocus();return true;}return false;}// Button事件監(jiān)聽,當(dāng)點擊第一個按鈕時增加一個button和一個textviewprivate Button.OnClickListener mClickListener = new Button.OnClickListener() {private int index = 1;@Overridepublic void onClick(View v) {TextView tView = new TextView(Test_ScrollView.this);//定義一個TextViewtView.setText("TextView" + index);//設(shè)置TextView的文本信息//設(shè)置線性布局的屬性LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);mLayout.addView(tView, params);//添加一個TextView控件Button button = new Button(Test_ScrollView.this);//定義一個Buttonbutton.setText("Button" + index);//設(shè)置Button的文本信息button.setId(index++);mLayout.addView(button, params);//添加一個Button控件mHandler.post(mScrollToButton);//傳遞一個消息進行滾動}};private Runnable mScrollToButton = new Runnable() {@Overridepublic void run() {int off = mLayout.getMeasuredHeight() - sView.getHeight();if (off > 0) {sView.scrollTo(0, off);//改變滾動條的位置}}};}

    此示例中一個TextView和一個Button來實現(xiàn)自動滾動,當(dāng)我們點擊Button0時自動產(chǎn)生多個類似的項,如果一屏顯示不完,則通過ScrollView來顯示。

    總結(jié)

    以上是生活随笔為你收集整理的Android学习笔记:ScrollView卷轴视图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。