網格視圖(GridView)
GridView是按照行列的形式來顯示內容,一般用于圖片、圖示的顯示,GridView 也可以像第4章介紹ListView一樣,以串列的形示來顯示內容,首先來看一下GridView的一些基本屬性,
- android:columnWidth :GridView中每一列的寬度
- android:gravity : 控制元件上面資訊的位置
- android:numColumns : 設定GridView的列數
- android:horizontalSpacing : 設定水平之間的間距
- android:verticalSpacing :設定垂直之間的間距
如果要顯示內容,同樣要為GridView設定一個配接器(Adapter) ,在這里我們也是寫一個類繼承BaseAdapter類,重寫其中的方法,主要是重寫getView()方法設定圖片的格式,下面通過一個簡單的實體來說明它的用法,運行效果如圖1所示,

圖1Gridview實體
在圖1中,以每行4個圖片排列,這可以通過android:numColumns屬性或代碼進行設定,所顯示的內容是通過一個配接器來提供的,然后通過GridView的setAdapter方法設定配接器,下面來看看配接器的實作:
//圖片陣列
private int[] images . (R. drawable. sample thumb_ 0, R. drawable. sample thumb_ 1
R. drawable.sample thumb 2, R.drawable.sample thumb 3, R. drawable.sample thumb_ 4,R.drawable.sample_ thumb 5, R.drawable.sample thumb 6, R. drawable.sample thumb_ 7,R.drawable.sample thumb 1,R.drawable.sample_ thumb_ 5, R.drawable. sample_ thumb_ 4,R. drawable.sample thumb 4, R. drawable.sample thumb_ 4);
private Context context;
public MyAdapter (Context context) {
this. context = context;
}
public int getCount () {
return images . length ;
}
public object getItem(int position) {
return position;
}
public long getItemId(int position) (return position;
public View getView(int position, View convertView, ViewGroup parent) {
lmageView imageView; //圖片控制元件
if (convertView = null) {
//實體化 ImageView 物件
imageView = new ImageView (context) ;
//設定ImageView物件的布局
imageView.setLayoutParams (nеw GridView. LayoutParams (45,45));
//設定邊界對齊
imageView. setAdjus tViewBounds (true) ;
imageView. setScaleType (ImageView ScaleType . CENTER CROP) ;
//設定間距
imageView.setPadding(8, 8, 8, 8);
)else(
imageView = (ImageView) convertView;
//這是ImageView設圖片資源
imageView. setImageResource (images[position]);
return imageView;
關于GriView的布局檔案如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns : android "http:/ 1 schemas . android . com/ apk/ res/android"
android: layout_ width="fill_parent"
android: layout height="fill_parent"
android: background="#ffffff"
android:orientation="vertical" >
<TextView
android: layout _width="fill_parent "
android: layout_ height="wrap content"
android:textSize="18dip"
android: textColor = "#000000"
android: text="GridView 實體">
<GridView
android:id="@+id/gridView"
android:layout_width="fill parent "
android: layout_ height="wrap_content"
android: horizontalSpacing="5dip"
android:verticalSpacing="5dip">
滾動視圖(ScrollView)
有時內容很多,一頁顯示不完,此時可以把有些控制元件加入到滾動視圖中,所以滾動視圖是指在內容很多,一螢屏顯示不完全的情況下,需要借助滾動來顯示內容,
例如,一個相冊,一頁顯示不完時,我們就可以使用滾動視圖,如圖2所示,這個界面只有一個按鈕,當我們不斷單擊這個按鈕時,就會增加一個TextView和一個Button, 隨著不斷的單擊,界面就會顯示一個滾動條,如圖2所示,這是因為整個布局檔案都放在了滾動視圖(ScrollView) 中,

圖2
下面來看看實作以上效果的代碼,
(1)布局檔案,此布局檔案最外面采用的ScrollView,嵌套了一個線性布局,按鈕和文本框都在線性布局中;
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xm1ns: android =http://schemas. android. com/apk/res/android"
android: id="@+id/ScrollView01"
android:layout_width="fill_parent"
android: layout_height="wrap_content"
android : background="#ffffff"
>
<LinearLayout xmlns: android="http://schemas . android. com/apk/ res/ android"
android:layout_ width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/LinearLayout01"
android :orientation="vertical" >
<TextView
android: layout_width="fill_parent"
android: layout_height="wrap content"
android:id="@+id/textview"
android: textSize="17sp"
android: textColor "#000000"
android: text-"ScrollView0" />
<Button
android:id="@+id/button"
android: layout_ width="fill_parent"
android: layout_height="wrap_content "
android: text="Button0"
/>
</ LinearLayout>
</ScrollView>
(2)按鈕事件的實作,單擊按鈕增加一個線性布局,這個布局檔案中添加了一個按鈕( Button)和文本框( TextView)
//按鈕事件:單擊按鈕增加一個文本框和按鈕
private OnClickListener listener = new OnClickListener (){
public void onClick (View v) {
TextView textView1 = new TextView (ScrollViewDemoActivity.this) ;
textView1.setText ("ScrollView"+index) ;
textView1.setTextcolor (Color.BLACK) ;
LinearLayout.LayoutParams layoutParams = new LinearLayout .
LayoutParams (LinearLayout.LayoutParams.FILL PARENT,Linearlayout.LayoutParams.WRAP_CONTENT) ;
layout.addView (textView1, layoutParams); //把TextView增加到布局中
//實體化一個按鈕
Button btn = new Button (ScrollViewDemoActivity. this) ;
btn. setText ("Button"+index) ;
//把Button增加到布局中
layout.addView (btn, layoutParams) ;
}
};
由上面的例子可以知道,ScrollView的主要作用是在一螢屏無法完全顯示時,可以考慮ScrollView,
本博主會持續更新Android學習知識,更多學習知識以及筆記請查看我的Gitee.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290022.html
標籤:其他
上一篇:初轉行測驗,竟敢拒絕 22K offer,這你能想象?
下一篇:一篇送給臨近35歲程式員的文章
