前一篇SlidingMenu学习(一)文章中了解了导入SlidingMenu到我们项目经常出现的问题,下面我们正式学习。
先看一个效果:
看到两幅图片的差别了吗,左边的一栏时可以滑动的,可以隐藏掉,现在就是用这个开源项目实现类似功能。
public class BaseActivity extends SlidingFragmentActivity
{
private int mtitleRes;
private Fragment frag; public BaseActivity(int mtitleRes)
{
this.mtitleRes=mtitleRes;
} @Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setTitle(mtitleRes); //设置behind View
this.setBehindContentView(R.layout.menu_frame);
if(savedInstanceState==null)
{
FragmentTransaction ft=this.getSupportFragmentManager().beginTransaction();
frag=new SimpleFragment();
ft.replace(R.id.menu_frame, frag);
ft.commit();
}else
{
frag=this.getSupportFragmentManager().findFragmentById(R.id.menu_frame); } SlidingMenu menu=this.getSlidingMenu();
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case android.R.id.home:
this.toggle();
break; }
return super.onOptionsItemSelected(item);
}
}
public class MainActivity extends BaseActivity { public MainActivity() {
super(R.string.main);
// TODO Auto-generated constructor stub
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); } }
相关的xml
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/main"
> </RelativeLayout>
menu_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/menu_frame"
> </FrameLayout>
simplefragment.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" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:src="@drawable/head"
/> </LinearLayout>
最后还是用一个Fragment.java
public class SimpleFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
System.out.println("onCreateViwe..............");
View view=inflater.inflate(R.layout.simplefragment, container, false);
return view;
}
}
效果图