Android_SlidingMenu详解

使用

  • 下载
  • 导入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>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值