您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了android點(diǎn)擊按鈕切換不同布局的具體代碼,供大家參考,具體內(nèi)容如下
先上效果圖:
如圖所示,實(shí)現(xiàn)點(diǎn)擊下面的按鈕切換不同的fragment布局;
不說(shuō)了,先上主MainActivity代碼:
MainActivity.java:
package com.example.xh.twostylefragment; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ //定義5個(gè)fragment private MyFragment f1; private MyFragment2 f2; private MyFragment3 f3; private MyFragment4 f4; private MyFragment5 f5; //定義底部5個(gè)按鈕 private Button foot1; private Button foot2; private Button foot3; private Button foot4; private Button foot5; int i = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); foot1 = (Button) findViewById(R.id.btn1);//注冊(cè)按鈕 foot2 = (Button) findViewById(R.id.btn2); foot3 = (Button) findViewById(R.id.btn3); foot4 = (Button) findViewById(R.id.btn4); foot5 = (Button) findViewById(R.id.btn5); foot1.setOnClickListener(this);//對(duì)按鈕設(shè)置監(jiān)聽(tīng) foot2.setOnClickListener(this); foot3.setOnClickListener(this); foot4.setOnClickListener(this); foot5.setOnClickListener(this); //第一次初始化首頁(yè)默認(rèn)顯示第一個(gè)fragment initFragment1(); } //顯示第一個(gè)fragment private void initFragment1(){ //開(kāi)啟事務(wù),fragment的控制是由事務(wù)來(lái)實(shí)現(xiàn)的 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); //第一種方式(add),初始化fragment并添加到事務(wù)中,如果為null就new一個(gè) if(f1 == null){ f1 = new MyFragment(); transaction.add(R.id.main_frame_layout, f1); } //隱藏所有fragment hideFragment(transaction); //顯示需要顯示的fragment transaction.show(f1); //第二種方式(replace),初始化fragment // if(f1 == null){ // f1 = new MyFragment("首頁(yè)"); // } // transaction.replace(R.id.main_frame_layout, f1); //提交事務(wù) transaction.commit(); } //顯示第二個(gè)fragment private void initFragment2(){ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if(f2 == null){ f2 = new MyFragment2(); transaction.add(R.id.main_frame_layout,f2); } hideFragment(transaction); transaction.show(f2); // if(f2 == null) { // f2 = new MyFragment("分類"); // } // transaction.replace(R.id.main_frame_layout, f2); transaction.commit(); } //顯示第三個(gè)fragment private void initFragment3(){ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if(f3 == null){ f3 = new MyFragment3(); transaction.add(R.id.main_frame_layout,f3); } hideFragment(transaction); transaction.show(f3); // if(f3 == null) { // f3 = new MyFragment("發(fā)現(xiàn)"); // } // transaction.replace(R.id.main_frame_layout, f3); transaction.commit(); } private void initFragment4(){ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if(f4 == null){ f4 = new MyFragment4(); transaction.add(R.id.main_frame_layout,f4); } hideFragment(transaction); transaction.show(f4); // if(f4 == null) { // f4 = new MyFragment("購(gòu)物車"); // } // transaction.replace(R.id.main_frame_layout, f4); transaction.commit(); } private void initFragment5(){ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if(f5 == null){ f5 = new MyFragment5(); transaction.add(R.id.main_frame_layout,f5); } hideFragment(transaction); transaction.show(f5); // if(f5 == null) { // f5 = new MyFragment("我的); // } // transaction.replace(R.id.main_frame_layout, f5); transaction.commit(); } //隱藏所有的fragment private void hideFragment(FragmentTransaction transaction){ if(f1 != null){ transaction.hide(f1); } if(f2 != null){ transaction.hide(f2); } if(f3 != null){ transaction.hide(f3); } if(f4 != null){ transaction.hide(f4); } if(f5 != null){ transaction.hide(f5); } } @Override public void onClick(View v) {//點(diǎn)擊哪個(gè)按鈕就顯示哪個(gè)fragment; if(v == foot1){ initFragment1(); }else if(v == foot2){ initFragment2(); }else if(v == foot3){ initFragment3(); }else if(v == foot4){ initFragment4(); }else if(v == foot5){ initFragment5(); } } }
大家需要?jiǎng)?chuàng)建5個(gè)fragment,還有對(duì)應(yīng)的xml文件,這里我給大家展示我創(chuàng)建的MyFragment4.java:
package com.example.xh.twostylefragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Administrator on 2016/7/8. */ public class MyFragment4 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = LayoutInflater.from(getActivity()).inflate(R.layout.shoppingcar,container,false);//用view保存shoppingcar.xml布局,大家可以 return view; //自己創(chuàng)建一個(gè)xml. } }
差不多就是這樣的了,有問(wèn)題大家可以提出來(lái)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。