Android学习笔记——简单的侧滑栏

本文是看了http://blog.csdn.net/leejizhou/article/details/52013343之后的学习笔记


(1):使用v4包下的SlidingPaneLayout

布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/SlidingPane"
    android:background="#000000"
    >

    <LinearLayout
        android:id="@+id/menu"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:background="#000000"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu1"
            android:textSize="20sp"
            android:textColor="#ffffff"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu2"
            android:textSize="20sp"
            android:textColor="#ffffff"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu3"
            android:textSize="20sp"
            android:textColor="#ffffff"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu4"
            android:textSize="20sp"
            android:textColor="#ffffff"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Hello World"
            android:textSize="50sp"
            android:gravity="center"
            />

    </LinearLayout>


</android.support.v4.widget.SlidingPaneLayout>
SlidingPaneLayout下有两个LenearLayout,第一个是menu,第二个是main

MainActivity代码如下:

public class MainActivity extends AppCompatActivity {

    private SlidingPaneLayout mSlidingPane;
    private LinearLayout main,menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSlidingPane = (SlidingPaneLayout) findViewById(R.id.SlidingPane);
        main = (LinearLayout) findViewById(R.id.main);
        menu = (LinearLayout) findViewById(R.id.menu);

        //mSlidingPane.setParallaxDistance();——>这是设置滑动视差
        //mSlidingPane.setCoveredFadeColor();——>这是设置Menu部分的颜色渐变效果
        //mSlidingPane.setSliderFadeColor();——>这是设置Main部分的颜色渐变效果


        mSlidingPane.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
            @Override
            public void onPanelSlide(View panel, float slideOffset) {

            }

            @Override
            public void onPanelOpened(View panel) {

            }

            @Override
            public void onPanelClosed(View panel) {

            }
        });


    }
}
此外还有openPane()和closePane()函数用来打开和关闭Menu部分

(2)使用DrawerLayout

下面是布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="50sp"
            android:text="Hello World"
            android:gravity="center"/>

    </LinearLayout>



    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:background="#ffff3f"
        android:layout_gravity="left">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="25sp"
            android:text="Menu"
            android:gravity="center"/>

    </LinearLayout>


</android.support.v4.widget.DrawerLayout>
DrawerLayout下有两个布局,第一个是Main布局(主体),第二个是Menu布局(也就是侧滑栏),需要给侧滑栏布局配置layout_gravity属性,不然识别不出来侧滑栏。属性可以使left/start表示从左边滑出,或者right/end表示从右边滑出。这个属性在我的AS上不会自动提示,不过手打就好了。。。

addDrawerListener(DrawerLayout.DrawerListener listener) 添加滑动监听
openDrawer(int gravity) 开启导航菜单 参数:GravityCompat.START或者GravityCompat.END 要跟 XML 设置相同
closeDrawer(int gravity) 关闭导航菜单
isDrawerOpen(int drawerGravity) 菜单是否开启
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值