首页 > 代码库 > 替代ActivityGroup的Fragment,实现主界面
替代ActivityGroup的Fragment,实现主界面
Fragment低版本使用需要导入V4包
主类文件:(因为低版本Activity中无法调用getFragmentManager()方法,所以继承FragmentActivity)
import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.view.View;import android.widget.Button;import com.example.testfragment.fragment.FragmentOne;import com.example.testfragment.fragment.FragmentTwo;public class FragmentActivityTest extends FragmentActivity { public String test = "test"; public Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_activity); getWidget(); } //获取控件并添加单击事件 private void getWidget() { button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentOne fragmentOne = new FragmentOne(); getSupportFragmentManager().beginTransaction().replace(R.id.framelayout, fragmentOne).commit(); } }); Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentTwo fragmentOne = new FragmentTwo(); getSupportFragmentManager().beginTransaction().replace(R.id.framelayout, fragmentOne).commit(); } }); }}
fragment_activity布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="48dp" android:background="#ff201bff" android:id="@+id/top_layout"></RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="48dp" android:background="#ff201bff" android:id="@+id/bottom_layout1" android:layout_alignParentBottom="true" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:text="界面1" android:layout_alignParentLeft="true"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:layout_alignParentRight="true" android:text="界面2"/> </RelativeLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/framelayout" android:layout_below="@id/top_layout" android:layout_above="@id/bottom_layout1" /></RelativeLayout>
两个Fragment文件以及简单布局:
1、
在Fragment 中可以利用方法getActivity()获取所依托的activity,然后即可获取activity中的数据
import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.example.testfragment.FragmentActivityTest;import com.example.testfragment.R;public class FragmentOne extends Fragment { private FragmentActivityTest activity; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_one, container, false); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); activity = (FragmentActivityTest) getActivity(); activity.button1.setText(activity.test); }}
R.layout.fragment_one布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="界面1" android:id="@+id/textView" android:layout_gravity="center_horizontal"/></LinearLayout>
2、
import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.example.testfragment.R;public class FragmentTwo extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_two, container, false); return view; }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="界面2" android:id="@+id/textView" android:layout_gravity="center_horizontal"/></LinearLayout>
替代ActivityGroup的Fragment,实现主界面
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。