Fragment入门
1、视图
1)主视图
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" 6 tools:context=".MainActivity" > 7 8 <fragment 9 android:id="@+id/fragment1" 10 android:name="com.example.myfragment.Fragment1" 11 android:layout_width="0dip" 12 android:layout_height="match_parent" 13 android:layout_weight="1" 14 /> 15 <fragment 16 android:id="@+id/fragment2" 17 android:name="com.example.myfragment.Fragment2" 18 android:layout_width="0dip" 19 android:layout_height="match_parent" 20 android:layout_weight="1" 21 /> 22 23 </LinearLayout>
2)fragment1视图
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/bt" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="修改左边" /> 12 13 </LinearLayout>
3)fragment2视图
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textView1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="我是文本" /> 12 13 </LinearLayout>
2、Fragment1的代码
1 package com.example.myfragment; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.view.ViewGroup; 9 import android.widget.Button; 10 11 public class Fragment1 extends Fragment { 12 13 @Override 14 public View onCreateView(LayoutInflater inflater, ViewGroup container, 15 Bundle savedInstanceState) { 16 // TODO Auto-generated method stub 17 View view = inflater.inflate(R.layout.fragment1, null); 18 Button button = (Button) view.findViewById(R.id.bt); 19 button.setOnClickListener(new OnClickListener(){ 20 21 @Override 22 public void onClick(View v) { 23 // TODO Auto-generated method stub 24 System.out.println("被点击了"); 25 Fragment2 fragment2 = (Fragment2) getActivity().getFragmentManager().findFragmentById(R.id.fragment2); 26 fragment2.setText("HAHAHA"); 27 } 28 29 }); 30 return view; 31 } 32 33 }
3、Fragment2的代码
1 package com.example.myfragment; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.TextView; 9 10 public class Fragment2 extends Fragment { 11 private TextView tv; 12 @Override 13 public View onCreateView(LayoutInflater inflater, ViewGroup container, 14 Bundle savedInstanceState) { 15 // TODO Auto-generated method stub 16 View view = inflater.inflate(R.layout.fragment2, null); 17 tv = (TextView) view.findViewById(R.id.textView1); 18 return view; 19 } 20 21 public void setText(String text){ 22 tv.setText(text); 23 } 24 25 }

浙公网安备 33010602011771号