一个Fragment的问题搞了好几天了,一直搞不明白是怎么回事,主要是想在低版本2.2上跑android3.0的效果,找了好几处例子,下载下来Demo就是打不到效果,不是这个出问题就是那个出问题,今天总算搞定了一个简单的小应用了,从别人那下载下来编译老出问题,最后还是修改成功了,不过还是没有达到自己想要的效果,先记载下来吧。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 = "cn.fuli.damai.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 = "cn.fuli.damai.SecondFragment" android:id = "@+id/secondFragment" android:layout_weight = "1" android:layout_width = "0px" android:layout_height = "wrap_content" /> </LinearLayout> </LinearLayout>
public class FragmentTestActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); FragmentManager fm = getSupportFragmentManager(); 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 = getSupportFragmentManager().beginTransaction(); ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out); if (fragment.isHidden()) { ft.show(fragment); button.setText("隐藏"); } else { ft.hide(fragment); button.setText("显示"); } ft.commit(); } }); } }
最后的就是有关Fragment的两个类了 public 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 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; } }
<?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> 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>