Fragment动画效果

1. 引言:

 

    Honeycomb3.0中,在Fragment添加或移除时为其添加画动效果。

 

2. 功能实现:

 

    (1) 主布局(main.xml)实现: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > <LinearLayout android:orientation = "horizontal" android:padding = "4dip" android:gravity = "center_vertical" android:layout_weight = "1" android:layout_width = "match_parent" android:layout_height = "wrap_content" > <Button android:id = "@+id/firstButton" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Hide" /> <fragment android:name = "com.flora.FragmentAnimationActivity$FirstFragment" android:id = "@+id/firstFragment" android:layout_weight = "1" android:layout_width = "0px" android:layout_height = "wrap_content" /> </LinearLayout> <LinearLayout android:orientation = "horizontal" android:padding = "4dip" android:gravity = "center_vertical" android:layout_weight = "1" android:layout_width = "match_parent" android:layout_height = "wrap_content" > <Button android:id = "@+id/secondButton" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Hide" /> <fragment android:name = "com.flora.FragmentAnimationActivity$SecondFragment" android:id = "@+id/secondFragment" android:layout_weight = "1" android:layout_width = "0px" android:layout_height = "wrap_content" /> </LinearLayout> </LinearLayout>

    (2) FirstFragment布局(first_fragment.xml)实现:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:padding = "4dip" android:layout_width = "match_parent" android:layout_height = "wrap_content" > <TextView android:id = "@+id/msg" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_weight = "0" android:paddingBottom = "4dip" /> </LinearLayout>

 

    (3) SecondFragment布局(second_fragment.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" > <TextView android:id = "@+id/msg" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_weight = "0" android:paddingBottom = "4dip" /> </LinearLayout>

 

    (4) 主Activity实现:package com.flora; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class FragmentAnimationActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); FragmentManager fm = getFragmentManager(); addListener(R.id.firstButton, fm.findFragmentById(R.id.firstFragment)); addListener(R.id.secondButton, fm.findFragmentById(R.id.secondFragment)); } private void addListener(int buttonID, final Fragment fragment) { final Button button = (Button)findViewById(buttonID); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out); if (fragment.isHidden()) { ft.show(fragment); button.setText("隐藏"); } else { ft.hide(fragment); button.setText("显示"); } ft.commit(); } }); } public static class FirstFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.first_fragment, container, false); TextView tv = (TextView)v.findViewById(R.id.msg); tv.setText("This is first fragment."); tv.setBackgroundColor(Color.GREEN); return v; } } public static class SecondFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.second_fragment, container, false); TextView tv = (TextView)v.findViewById(R.id.msg); tv.setText("This is second fragment."); tv.setBackgroundColor(Color.RED); return v; } } }

 

 

 

你可能感兴趣的:(android,layout,Class,button,encoding)