Android自3.0版本开始,为了适应平板的需要,引入了fragment。由于学习时间较长,几乎忘了怎么使用了。今天看了《第一行代码》中的例子,重新对着敲了一遍,在此记录,以便日后的不时之需。
通过xml文件的方式添加fragment。
先是fragment_left.xml
<pre name="code" class="html"><?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" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="显示" />
</LinearLayout>
其次fragment_right.xml
<pre name="code" class="html"><?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="#00ff00" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="This is right fragment."
android:textSize="20sp" />
</LinearLayout>
再是LeftFragment.java
package com.dt.bitmapoptionsd.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.dt.bitmapoptionsd.R;
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return View.inflate(getActivity(), R.layout.fragment_left, null);
}
}
再是RightFragment.java
package com.dt.bitmapoptionsd.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.dt.bitmapoptionsd.R;
public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return View.inflate(getActivity(), R.layout.fragment_right, null);
}
}
再是fragment_layout.xml
<pre name="code" class="html"><?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" >
<fragment
android:id="@+id/left_fragment"
android:name="com.dt.bitmapoptionsd.fragment.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> <fragment
android:id="@+id/right_fragment"
android:name="com.dt.bitmapoptionsd.fragment.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
最后是FragmentTest.java
public class FragmentTest extends Activity implements OnClickListener{
private Button btn;
private boolean isGreen = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
}
通过代码的方式动态的替换fragment。
先修改一下fragment_layout.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" >
<fragment
android:id="@+id/left_fragment"
android:name="com.dt.bitmapoptionsd.fragment.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<fragment
android:id="@+id/right_fragment"
android:name="com.dt.bitmapoptionsd.fragment.RightFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
再新建一个fragment_right_two.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="#ffff00" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="This is right fragment."
android:textSize="20sp" />
</LinearLayout>
对应的SecondRightFragment.java
package com.dt.bitmapoptionsd.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.dt.bitmapoptionsd.R;
public class SecondRightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_right_two, container, false);
}
}
最后的FragmentTest.java。其中为了当点击返回键的时候,实现像activity一样的退栈效果,而不是直接退出应用,调用了transaction的addToBackStack方法。
package com.dt.bitmapoptionsd.fragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import com.dt.bitmapoptionsd.R;
public class FragmentTest extends Activity implements OnClickListener{
private Button btn;
private boolean isGreen = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn)
{
FragmentManager manager = getFragmentManager();
SecondRightFragment secondfragment = new SecondRightFragment();
RightFragment fragment = new RightFragment();
if(isGreen)
{
manager.beginTransaction().replace(R.id.right_layout, secondfragment).addToBackStack(null).commit();
}else
{
manager.beginTransaction().replace(R.id.right_layout, fragment).addToBackStack(null).commit();
}
isGreen = !isGreen;
}
}
}