1. 程式人生 > >底部選單欄 Fragment + button 鍵切換

底部選單欄 Fragment + button 鍵切換

1,實現介面setOnClickListener 重寫裡面onClick 方法 

2,設定點選事件

3,用switch  獲取控制元件ID 。設定點選事件。

【1】佈局:

<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=".MainActivity" >



    <LinearLayout

        android:id="@+id/ll_layout"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical" >

    </LinearLayout>



    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:orientation="horizontal" >



        <Button

            android:id="@+id/btn_wx"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="微信" />



        <Button

            android:id="@+id/btn_contact"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="通訊錄" />



        <Button

            android:id="@+id/btn_disconver"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="發現" />



        <Button

            android:id="@+id/btn_me"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="我" />

    </LinearLayout>



</RelativeLayout>

【2】程式碼:

public class MainActivity extends Activity implements OnClickListener {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // [1]找到控制元件

        Button btn_contact = (Button) findViewById(R.id.btn_contact);

        Button btn_disconver = (Button) findViewById(R.id.btn_disconver);

        Button btn_me = (Button) findViewById(R.id.btn_me);

        Button btn_wx = (Button) findViewById(R.id.btn_wx);

        // [2]設定點選事件

        btn_contact.setOnClickListener(this);

        btn_disconver.setOnClickListener(this);

        btn_me.setOnClickListener(this);

        btn_wx.setOnClickListener(this);

        

    }



    @Override

    public void onClick(View v) {

        //[2.1]獲取fragment的管理者

        FragmentManager fragmentManager = getFragmentManager();

        FragmentTransaction beginTransaction = fragmentManager.beginTransaction();

        

        // [3]具體判斷一下點選的是哪個按鈕

        switch (v.getId()) {

        case R.id.btn_wx:   //點選微信按鈕 動態替換fragment

            beginTransaction.replace(R.id.ll_layout, new WxFragment());

            

            break;



        case R.id.btn_contact:

            beginTransaction.replace(R.id.ll_layout, new ContactFragment());



            break;

        case R.id.btn_disconver:

            beginTransaction.replace(R.id.ll_layout, new DiscoverFragment());

            break;



        case R.id.btn_me:

            beginTransaction.replace(R.id.ll_layout, new MeFragment());

            break;

        }

        //[4]最後一步記得commit

beginTransaction.commit();



    }



}