安卓開發3-自訂安卓組件titlebar

來源:互聯網
上載者:User

標籤:

1,titlebar

    button  + textview實現

    在layout檔案夾內建立titlebar.xml檔案

           建立一個水平linearlayout  ,將button與text放進去,根絕上一節的 自適應特性 設定 layout-width 與layout-height  值 例如 @dimner/x333

2,放入主布局中

    在朱布局代碼中加入<include layout="@檔案夾名/xml檔案名稱">在其中也可以加入id之類的屬性

    本文中為<include layout="@layout/titlebar"/>  

3,資料繫結

    就是把xml布局中的組件找出來,用java代碼給其賦值並顯示。就是jQuery的docment.getIDby("xxx").value之類的意思

  下面是設計

    第一個就是button onclick事件的設計

        自然而然,點擊了這個按鈕就是使用者看到了頁面回去了(返回按鈕),頁面怎麼回去先不管,activity或者fragment,這個另說

    第二個就是 textview裡內容的設定,轉到不同的頁面,頁面抬頭自然不一樣。

  所以就是以上兩點(初步設想,方案會有後續改動)

··    (1)看你繫結資料是啥,簡單的arrayadapter,simpleadapter就行

      複雜的就extends baseAdapter

      我的理解是,首先確定資料來源,要綁定組件對象,看用哪種adapter

或者是 放進去 綁定對象 的 是一個自訂的組件 ,除了要 顯示這個自訂的組件 外 ,還要把資料綁到 這個自訂的組件 裡

 

      

 

 

     (2)public class NineSquareAdaper extends BaseAdapter

package com.example.administrator.oop;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;/** * Created by Administrator on 2015/6/25. */public class NineSquareAdaper extends BaseAdapter {    private Context context;    public NineSquareAdaper(Context context) {        this.context=context;    }    private Integer[] images = {            //九宮格圖片的設定            R.drawable.icon01,            R.drawable.icon02,            R.drawable.icon03,            R.drawable.icon04,            R.drawable.icon05,            R.drawable.icon06,            R.drawable.icon07,            R.drawable.icon08,            R.drawable.icon09,    };    private String[] texts = {            //九宮格圖片下方文字的設定            "記錄支出",            "記錄收入",            "賬本管理",            "類別管理",            "查看圖表",            "收支對照",            "記錄心得",            "新聞公告",            "系統設定",    };    @Override    // 1  當系統開始繪製ListView的時候,首先調用getCount()方法。得到它的傳回值,即ListView的長度    public int getCount() {        return images.length;    }//getItem()和getItemId()則在需要處理和取得Adapter中的資料時調用。    @Override    public Object getItem(int position) {        return position;    }    @Override    public long getItemId(int position) {        return position;    }    @Override    // 2 根據getCount()返回長度逐一繪製ListView的每一行    public View getView(int position, View convertView, ViewGroup parent) {        ImgTextWrapper wrapper;        if(convertView==null) {            wrapper = new ImgTextWrapper();//            在實際開發中LayoutInflater這個類還是非常有用的,它的作用類似於findViewById()。不同點是LayoutInflater是用來找res/layout/下的xml布局檔案,並且執行個體化;而findViewById()是找xml布局檔案下的具體widget控制項(如Button、TextView等)。//            具體作用://            1、對於一個沒有被載入或者想要動態載入的介面,都需要使用LayoutInflater.inflate()來載入;//            2、對於一個已經載入的介面,就可以使用Activiyt.findViewById()方法來獲得其中的介面元素            //http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html            LayoutInflater inflater = LayoutInflater.from(context);            convertView = inflater.inflate(R.layout.squares, null);            convertView.setTag(wrapper);            convertView.setPadding(15, 15, 15, 15);  //每格的間距        } else {            wrapper = (ImgTextWrapper)convertView.getTag();        }        wrapper.imageView = (ImageView)convertView.findViewById(R.id.ItemImage);        wrapper.imageView.setBackgroundResource(images[position]);        wrapper.textView = (TextView)convertView.findViewById(R.id.ItemText);        wrapper.textView.setText(texts[position]);        return convertView;    }}class ImgTextWrapper {    ImageView imageView;    TextView textView;}

  

        

安卓開發3-自訂安卓組件titlebar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.