fragment例子

来源:互联网 发布:厦门华厦学院网络管理 编辑:程序博客网 时间:2024/05/17 06:06

main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:layout_width="fill_parent"android:layout_height="fill_parent" android:id="@+id/rootView"><LinearLayout android:id="@+id/leftView"android:orientation="vertical" android:layout_width="0dp"android:layout_weight="1" android:layout_height="fill_parent"android:background="#c0c0c0c0"/><LinearLayout android:id="@+id/rightView"android:orientation="vertical" android:layout_width="0dp"android:layout_weight="2" android:layout_height="fill_parent" /></LinearLayout>

demo_left_fragment.xml

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="wrap_content"android:text="@string/textview" />

FragmentTestActivity.java

package com.example.myfragment;import android.app.Activity;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;public class FragmentTestActivity extends FragmentActivity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        FragmentManager fragmentManager=getSupportFragmentManager();        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();        DemoLeftFragment leftFragment=new DemoLeftFragment();        //DemoRightFramgment rightFramgment=new DemoRightFramgment();        fragmentTransaction.add(R.id.leftView, leftFragment);        //fragmentTransaction.add(R.id.rightView, rightFramgment);        fragmentTransaction.commit();    }}

DemoLeftFragment.java

package com.example.myfragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class DemoLeftFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.demo_left_fragment, container,false);}}


原创粉丝点击