标签:
主界面layout文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/activity_main2" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 tools:context="com.example.dbwater.myapplication.Main2Activity"> 10 11 <LinearLayout 12 android:layout_width="match_parent" 13 android:layout_height="0dp" 14 android:layout_weight="1" 15 android:id="@+id/layout_fragment" 16 android:orientation="horizontal"></LinearLayout> 17 <LinearLayout 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 > 21 <Button 22 android:layout_width="0dp" 23 android:layout_height="match_parent" 24 android:layout_weight="1" 25 android:text="首页" 26 android:onClick="bt1_onclick" 27 android:id="@+id/bt1"/> 28 <Button 29 android:layout_width="0dp" 30 android:layout_height="match_parent" 31 android:layout_weight="1" 32 android:text="购买" 33 android:onClick="bt1_onclick" 34 android:id="@+id/bt2"/> 35 <Button 36 android:layout_width="0dp" 37 android:layout_height="wrap_content" 38 android:layout_weight="1" 39 android:text="个人简介" 40 android:onClick="bt1_onclick" 41 android:id="@+id/bt3"/> 42 </LinearLayout> 43 </LinearLayout>
java主函数
1 package com.example.dbwater.myapplication; 2 3 import android.support.v4.app.FragmentActivity; 4 import android.support.v4.app.FragmentManager; 5 import android.support.v7.app.AppCompatActivity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Toast; 9 10 public class Main2Activity extends FragmentActivity { 11 12 MainFragment mMainFragment; 13 BuyFragment mBuyFragment; 14 PersionFragment mPersionFragment; 15 FragmentManager mFragmentManager; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main2); 20 mMainFragment=new MainFragment(getApplicationContext()); 21 mFragmentManager=getSupportFragmentManager(); 22 mFragmentManager.beginTransaction().add(R.id.layout_fragment,mMainFragment).commit(); 23 } 24 public void bt1_onclick(View v) 25 { 26 switch (v.getId()) 27 { 28 case R.id.bt1: 29 mFragmentManager.beginTransaction().replace(R.id.layout_fragment,mMainFragment).commit(); 30 Toast.makeText(getApplicationContext(), "首页", Toast.LENGTH_SHORT).show(); 31 32 break; 33 case R.id.bt2: 34 mBuyFragment = new BuyFragment(getApplicationContext()); 35 mFragmentManager.beginTransaction().replace(R.id.layout_fragment,mBuyFragment).commit(); 36 Toast.makeText(getApplicationContext(), "购买", Toast.LENGTH_SHORT).show(); 37 break; 38 case R.id.bt3: 39 mPersionFragment=new PersionFragment(getApplicationContext()); 40 mFragmentManager.beginTransaction().replace(R.id.layout_fragment,mPersionFragment).commit(); 41 Toast.makeText(getApplicationContext(), "个人", Toast.LENGTH_SHORT).show(); 42 43 break; 44 } 45 } 46 }
首页Fragment的Java类:
1 package com.example.dbwater.myapplication; 2 3 import android.content.Context; 4 import android.os.Bundle; 5 import android.support.annotation.Nullable; 6 import android.support.v4.app.Fragment; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 11 /** 12 * Created by Administrator on 2016/7/28. 13 */ 14 15 public class MainFragment extends Fragment { 16 private Context mContext; 17 public MainFragment(Context context) 18 { 19 mContext=context; 20 } 21 @Nullable 22 @Override 23 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 24 View view=LayoutInflater.from(mContext).inflate(R.layout.fragment_main,null); 25 return super.onCreateView(inflater, container, savedInstanceState); 26 } 27 }
标签:
原文地址:http://www.cnblogs.com/beens/p/5714290.html