本文實例為大家分享了Android實現Gallery畫廊的具體代碼,供大家參考,具體內容如下
成都創新互聯公司服務項目包括紅旗網站建設、紅旗網站制作、紅旗網頁制作以及紅旗網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,紅旗網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到紅旗省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!
僅是實現基本功能,頁面粗糙請見諒
圖片下標0開始
activity_main.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.gallery.MainActivity"> <Gallery android:id="@+id/galPicture" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
GalleryAdapter.java頁面:
package com.example.gallery; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class GalleryAdapter extends BaseAdapter { private Context mContext; int[] images = {R.mipmap.apple, R.mipmap.banana, R.mipmap.bicycle, R.mipmap.chair,R.mipmap.chopsticks, R.mipmap.dog, R.mipmap.fish, R.mipmap.pear}; //本地圖片 public GalleryAdapter (Context context) { this.mContext = context; } @Override public int getCount() { return images.length; } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ImageView image = new ImageView(mContext); image.setImageResource(images[i]); //設置圖片 image.setAdjustViewBounds(true); //是否調整邊框 image.setLayoutParams(new Gallery.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return image; } }
MainActivity.java頁面:
package com.example.gallery; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.Toast; public class MainActivity extends AppCompatActivity { GalleryAdapter galleryAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Gallery galPicture = findViewById(R.id.galPicture); galleryAdapter = new GalleryAdapter(MainActivity.this); galPicture.setAdapter(galleryAdapter); //相應的點擊事件 galPicture.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, "圖片" + i, Toast.LENGTH_LONG).show(); } }); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創新互聯。
名稱欄目:Android實現漂亮的Gallery畫廊
當前路徑:http://m.kartarina.com/article46/pihshg.html
成都網站建設公司_創新互聯,為您提供App開發、網站策劃、微信公眾號、品牌網站設計、搜索引擎優化、標簽優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯