Android自訂“圖片+文字”控制項四種實現方法之一——–Gallery原理(提供源碼下載)

來源:互聯網
上載者:User

要想做圖片+文字這種複合控制項,實現方法大概有四種。第一種就是利用Gallery來做。

第一部分:建立一個布局檔案,用來放圖片加文字。名字為:pic_text.xml,內容為:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><ImageViewandroid:id="@+id/image"android:layout_gravity="center_horizontal"android:layout_width="80dp"android:layout_height="80dp"/><TextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:textSize="14dp"android:gravity="center"android:textColor="#ffffffff"/></LinearLayout>

第二部分:整個程式的布局檔案,也就是一個gallery:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <Gallery         android:id="@+id/myGallery"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/></LinearLayout>

第三部分:主程式:

package yan.guoqi.testgallery;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class TestGalleryActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Gallery gallery = (Gallery)findViewById(R.id.myGallery);        gallery.setAdapter(new galleryAdapter(this));        gallery.setOnItemClickListener(new OnItemClickListener() {            public void onItemClick(AdapterView<?> parent, View v, int position,                    long id) {                // TODO Auto-generated method stub                Toast.makeText(TestGalleryActivity.this,                        ""+id+"被點擊!",                        Toast.LENGTH_SHORT).show();                            }        });        gallery.setSelection(1);        gallery.setSpacing(20);        gallery.setUnselectedAlpha(150.0f);    }    public class galleryAdapter extends BaseAdapter{        private Integer[] img = {R.drawable.identify,                R.drawable.recognize,R.drawable.manage};        private String[] str={"認證模組","識別模組","管理掌紋庫"};        private Context mContext;        public galleryAdapter(Context c){            mContext = c;                    }                            public int getCount() {            // TODO Auto-generated method stub            return img.length;        }        public Object getItem(int position) {            // TODO Auto-generated method stub            return position;        }        public long getItemId(int position) {            // TODO Auto-generated method stub            return position;        }        public View getView(int position, View convertView, ViewGroup parent) {            // TODO Auto-generated method stub            ViewHolder holder;            if(convertView == null){                holder = new ViewHolder();                convertView = View.inflate(mContext, R.layout.pic_text, null);                holder.pic = (ImageView)convertView.findViewById(R.id.image);                holder.text = (TextView)convertView.findViewById(R.id.text);                convertView.setTag(holder);                            }            else{                holder = (ViewHolder)convertView.getTag();            }            holder.pic.setImageResource(img[position]);            holder.text.setText(str[position]);            return convertView;        }                class ViewHolder {            private ImageView pic;            private TextView text;            }            }}

(圖截的有點小,⊙﹏⊙b汗):

分析:這種實現方式是藉助Gallery,自訂一個布局。要自己寫適配器,對於學習gallery還算不錯。做出來的效果可以滑動。但不推薦這麼寫,這麼寫貌似大寫小用Gallery了。嚴重推薦下篇部落格裡的寫法。

源碼--http://download.csdn.net/detail/yanzi1225627/5108013

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.