Android简易实战教程--第四十四话《ScrollView和HorizontalScrollView简单使用》
生活随笔
收集整理的這篇文章主要介紹了
Android简易实战教程--第四十四话《ScrollView和HorizontalScrollView简单使用》
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、ScrollView
由于手機屏幕的高度有限,當普通布局放不下現實和的內容時,ScrollView視圖(滾動視圖)就會派上用場,因為數據可以往下滾動顯示。
二、HorizontalScrollView
看名稱就清楚,當想在水平方向想放置更多的空間,屏幕寬度放不下的時候,它就派上用場了。因為用法非常簡單,跟ScrollView一樣只不過是個父容器,所以我結合上面的案例,把HorizontalScrollView放到了ScrollView里面。這樣通過一個案例,同時學習了兩個組件的使用,機智如我~
為了程序的可讀性,直接上完整代碼了:
總布局: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/myscroll"android:layout_width="fill_parent"android:layout_height="fill_parent" ><LinearLayoutandroid:id="@+id/mylinear"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical" ><ImageViewandroid:layout_width="match_parent"android:layout_height="150dp"android:scaleType="centerCrop"android:src="@drawable/recommend_61" /><HorizontalScrollViewandroid:background="#99cccccc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:scrollbars="none" ><LinearLayoutandroid:id="@+id/id_gallery"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:orientation="horizontal" ></LinearLayout></HorizontalScrollView></LinearLayout><!-- android:scrollbars="none" --></ScrollView>因為我要在HorizontalScrollView的基礎上,繼續嵌套控件,因此定義了一個item的布局:activity_index_gallery_item.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="200dp"android:orientation="vertical" ><ImageViewandroid:id="@+id/id_index_gallery_item_image"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="5dp"android:scaleType="centerCrop"android:src="@drawable/home01" /><TextViewandroid:id="@+id/id_index_gallery_item_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="哈哈"android:layout_gravity="center_horizontal"android:textColor="#ff0000"android:textSize="12dp" /></LinearLayout>最后活動中的代碼。稍作修改: package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends Activity { String str[] = { "1", "2", "3", "4", "5", "6", "7", "8", }; private int[] images = {R.drawable.home01,R.drawable.home02,R.drawable.home03,R.drawable.home04};public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); //初始設置按鈕initBtn(); //初始設置HorizontalScrollViewinitHorizontalScrollView();}private void initBtn() {//拿到線性布局容器LinearLayout linear = (LinearLayout) super.findViewById(R.id.mylinear);// 取得組件 //設置按鈕的大小參數LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);// 定義按鈕的布局參數 ,寬度充滿父容器,高度包裹內容for (int i = 0; i < str.length; i++) { //動態添加按鈕Button btn = new Button(this);// 創建按鈕組件 btn.setText(this.str[i]);// 設置文本 //給每個按鈕都設置id編號(可通過btn.getId()獲取對應的id編號)btn.setId(i); //參數設置給按鈕btn.setLayoutParams(params);linear.addView(btn);// 線性布局增加孩子組件 btn.setOnClickListener(new OnClickListenerImpl()); }} private void initHorizontalScrollView() {//HorizontalScrollView的孩子容器LinearLayout mGallery = (LinearLayout) findViewById(R.id.id_gallery); //給孩子容器添加控件for (int i = 0; i < images.length; i++) { //控件以布局的形式引入View view = View.inflate(this, R.layout.activity_index_gallery_item, null); //設置圖片資源數據ImageView img = (ImageView) view .findViewById(R.id.id_index_gallery_item_image);// 找到顯示圖片的控件 img.setImageResource(images[i]); img.setId(i); TextView txt = (TextView) view .findViewById(R.id.id_index_gallery_item_text); txt.setText("我是圖片"+i); mGallery.addView(view); } }private class OnClickListenerImpl implements OnClickListener { public void onClick(View v) { switch (v.getId()) { case 0: Toast.makeText(MainActivity.this, "您選擇了按鈕1!", Toast.LENGTH_SHORT) .show(); break; case 1: Toast.makeText(MainActivity.this, "您選擇了按鈕2!", Toast.LENGTH_SHORT) .show(); break; case 2: Toast.makeText(MainActivity.this, "您選擇了按鈕3!", Toast.LENGTH_SHORT) .show(); break; case 3: Toast.makeText(MainActivity.this, "您選擇了按鈕4!", Toast.LENGTH_SHORT) .show(); break; case 4: Toast.makeText(MainActivity.this, "您選擇了按鈕5!", Toast.LENGTH_SHORT) .show(); break; case 5: Toast.makeText(MainActivity.this, "您選擇了按鈕6!", Toast.LENGTH_SHORT) .show(); break; case 6: Toast.makeText(MainActivity.this, "您選擇了按鈕7!", Toast.LENGTH_SHORT) .show(); break; case 7: Toast.makeText(MainActivity.this, "您選擇了按鈕8!", Toast.LENGTH_SHORT) .show(); break; default: break; } } } }
運行效果如下:
您如果是正處于學習階段,真誠的邀請您加入剛建立的群交流Android技術,一起學習:
Android開發交流群:497646615
也可以關注Android程序員開發指南? 公眾號您看一看文章也就5-10分鐘,筆者要花1個多小時才能完成,一起討論問題哈。公眾號二維碼:
轉載于:https://www.cnblogs.com/wanghang/p/6299504.html
總結
以上是生活随笔為你收集整理的Android简易实战教程--第四十四话《ScrollView和HorizontalScrollView简单使用》的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP 配置文件详解(php.ini 详
- 下一篇: 实变函数与泛函分析导论