Fragment demo

本文介绍了一种在Android应用中实现左右布局的方法,通过使用FragmentActivity结合两个Fragment(左侧和右侧Fragment),并详细展示了如何在Activity及XML布局文件中进行配置。

 left fragment:

package com.easymorse.demos;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DemoLeftFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.demo_left_fragment, container,false);
	}
}

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="demo left fragment" />


 


Right fragment:

package com.easymorse.demos;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DemoRightFramgment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.demo_right_fragment, container,false);
	}
}

demo_right_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="demo right fragment" />


 


Activity:

package com.easymorse.demos;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class FragmentDemoActivity extends FragmentActivity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
    //START.for first main.xml
		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();
    // END. }

}


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>

or 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">
 
 <fragment class= "com.easymorse.demos.DemoLeftFragment" android:id="@+id/leftView"
  android:orientation="vertical" android:layout_width="0dp"
  android:layout_weight="1" android:layout_height="fill_parent" 
  android:background="#c0c0c0c0"/>
  
 <fragment class="com.easymorse.demos.DemoRightFragment" android:id="@+id/rightView"
  android:orientation="vertical" android:layout_width="0dp"
  android:layout_height="fill_parent" android:layout_weight="1"/>
</LinearLayout>



 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值