[android] 介面切換的核心方法,android切換

來源:互聯網
上載者:User

[android] 介面切換的核心方法,android切換

根據拆分介面

 

 

View ==> ViewGroup ==> RelativeLayout,主體部分使用RelativeLayout作為佔位

 

View和ViewGroup的區別:ViewGroup有特有的addView()和removeView()方法,RelativeLayout添加進來View之後,後一個會覆蓋住前一個View;LinearLayout添加進View之後,會順序排放,不適合

 

設計原理:

樹形結構的資料處理,遵循組合設計模式

 

 

找到主體部分的RelativeLayout對象,添加進來View對象

兩秒後切換第二個介面,利用Handler發送延遲訊息類比切換

RelativeLayout對象,添加進來第二個View對象

 

問題:

 

MainActivity.java

    private Handler handler=new Handler(){        @Override        public void handleMessage(Message msg) {            loadMemberView();        }    };    /**     * 初始化主體     */    public void initMiddle() {        mainContainer=(RelativeLayout) findViewById(R.id.il_main);        loadLoginView();        handler.sendEmptyMessageDelayed(1, 2000);    }    /**     * 載入登陸介面     */    private void loadLoginView() {        View child=new IndexView(this).getLoginView();        mainContainer.addView(child);    }    /**     * 載入會員介面     */    private void loadMemberView() {        View child=new IndexView(this).getMemberView();        mainContainer.removeAllViews();        mainContainer.addView(child);    }

IndexView.java

package com.tsh.lottery.views;import android.content.Context;import android.view.View;import android.widget.TextView;/** * 介面 * @author taoshihan * */public class IndexView {    private Context context;        public IndexView(Context context) {        this.context = context;    }    /**     * 擷取登陸介面     * @return     */    public View getLoginView() {        TextView tv=new TextView(context);        tv.setText("我是登陸介面");        return tv;    }    /**     * 擷取會員介面     * @return     */    public View getMemberView() {        TextView tv=new TextView(context);        tv.setText("我是會員介面");        return tv;    }}

 

聯繫我們

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