溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

流動視圖ScrollView

發(fā)布時間:2020-05-18 08:32:41 來源:網(wǎng)絡(luò) 閱讀:366 作者:沒有水勒魚 欄目:移動開發(fā)

滾動視圖的使用形式與各個布局管理器的操作形式類似,唯一不同的是,所有的布局管理器之中,可以包含多個組件,而滾動視圖里只能有一個組件,所以所謂的視圖指的就是提供一個專門的容器,這個容器里面可以裝下多于屏幕寬度的組件,而后采用拖拽的方式顯示所有 ScrollView 中的組件。

  我們這個案例是顯示常用網(wǎng)址!

  知識點:ScrollView控件

一、設(shè)計界面

  1、打開“res/layout/activity_main.xml”文件。

    手工輸入以下代碼:切記XML文件ScrollView中只能放一個其他控件,如果想加入更多,只能通過java代碼形式。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<LinearLayout 
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >



</LinearLayout>
</ScrollView>


二、ScrollView流動視圖代碼 

  1、打開“src/com.genwoxue.scrollview/MainActivity.java”文件。

  然后輸入以下代碼:

package com.example.hw;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;




public class MainActivity extends Activity {
private String webaddress[] ={"網(wǎng)易:www.163.com","新浪:www.sina.com.cn","搜狐:www.sohu.com",
"騰訊:www.qq.com","百度:www.baidu.com","東方財富:www.eastmoney.com",
"金融界:www.jrj.com.cn","奇藝:www.iqiyi.com","攜程網(wǎng):www.ctrip.com","中國移動:www.10086.cn",
"美食中國:www.meishichina.com","工商銀行:www.icbc.com.cn","CSDN:www.csdn.net","跟我學(xué):www.genwoxue.com"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取LinearLayout布局
LinearLayout layout = (LinearLayout) super.findViewById(R.id.linear);
//定義布局參數(shù)
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for(int i = 0;i<this.webaddress.length;i++){
Button btnWebAddress = new Button(this);
btnWebAddress.setText(webaddress[i]);//設(shè)置顯示字體
layout.addView(btnWebAddress,param);//增加組件
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}



ScrollView繼承自FrameLayout,所以ScrollView控件本質(zhì)就是一個布局管理器。

  2、效果如下,可以上下滑動:

流動視圖ScrollView



向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI