Fragment进阶(一)----->静态

来源:互联网 发布:windows vista重装 编辑:程序博客网 时间:2024/05/29 16:10

MainActivity.java

package com.example.fragmenttest;import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <fragment        android:name="com.example.fragmenttest.MyFragment"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="match_parent" />    <fragment        android:name="com.example.fragmenttest.MyFragment2"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="match_parent" /></LinearLayout>

MyFragment.java

package com.example.fragmenttest;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class MyFragment extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return View.inflate(getActivity(), R.layout.fragment_article, null);}}

fragment_article.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     android:background="#EEE9E9">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="我是一个fragment界面" /></LinearLayout>

MyFragment2.java

package com.example.fragmenttest;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class MyFragment2 extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return View.inflate(getActivity(), R.layout.fragment_article2, null);}}

fragment_article2.xml


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="我是一个fragment界面2" /></LinearLayout>



0 1
原创粉丝点击