Android_SlidingMenu详解

来源:互联网 发布:plsql怎么执行sql文件 编辑:程序博客网 时间:2024/06/05 07:15

使用

  • 下载
  • 导入Android Studio
  • 修复错误
  • 开始编码

基本代码

效果


代码详解:

public class MainActivity extends AppCompatActivity {    private SlidingMenu menu;    private TextView menu_tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取Slidingmenu的对象        menu = new SlidingMenu(this);        //设置主页面剩余的宽度        menu.setBehindOffset(200);        //设置Slidingmenu的布局        menu.setMenu(R.layout.slidingmenu_layout);        //设置Slidingmenu从左侧滑出还是右侧        menu.setMode(SlidingMenu.LEFT_RIGHT);        //Slidingmenu要与Activity绑定        menu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);//        menu_tv = (TextView) findViewById(R.id.menu_tv);//        menu_tv.setOnClickListener(new View.OnClickListener() {//            @Override//            public void onClick(View view) {//                Toast.makeText(MainActivity.this, menu_tv.getText().toString(), Toast.LENGTH_SHORT).show();//            }//        });        getSupportFragmentManager().beginTransaction().replace(R.id.layout,new BlankFragment()).commit();    }}

public class BlankFragment extends Fragment {    public BlankFragment() {        // Required empty public constructor    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // Inflate the layout for this fragment        return inflater.inflate(R.layout.fragment_blank, container, false);    }}
activity_main:
<?xml version="1.0" encoding="utf-8"?><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="alice.bw.com.day06slidingmenu.MainActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!"/></RelativeLayout>
Fragment_b:
<FrameLayout 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:background="@android:color/holo_blue_light"             tools:context="alice.bw.com.day06slidingmenu.BlankFragment">    <!-- TODO: Update blank fragment layout -->    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="@string/hello_blank_fragment"/></FrameLayout>
SlidingMenu_Layout:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:background="@android:color/holo_red_light"              android:layout_width="match_parent"              android:layout_height="match_parent">    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/layout"></FrameLayout>    <TextView        android:id="@+id/menu_tv"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="侧滑菜单"/></LinearLayout>