ActionBar和DrawLayout和SlidingMenu

一.ToolBar常用的方法

Toolbar是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件,以此来取代之前的Actionbar 。我们需要在工程中引入appcompat-v7的兼容包以便向下兼容, 使用android.support.v7.widget.Toolbar进行开发。在设计 Toolbar 的时候,Google也留给了开发者很多可定制修改的余地,这些可定制修改的属性在API文档中都有详细介绍,如:

1.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);去掉标题栏;
2.Toolbar.setLogo(),设置logo图片;
3.Toolbar.setTitle().设置标题;
4.Toolbar.setSubTitle()设置子标题;
5.Toolbar.setTitleTextColor(int color);设置标题文字颜色;
6.Toolbar.setSubtitleTextColor();设置子标题文字颜色;
7.setTitleMargin(int start, int top, int end, int bottom);设置标题margin值; 8.onCreateOptionsMenu,getMenuInflater().inflate(R.menu.menu,menu)
设置菜单在给Toolbar设置为actionbar时使用;
9.Toolbar.setOnMenuItemClickListener();Toolbar绑定menu监听;
10.Toolbar.inflateMenu(R.menu.menu)在Toolbar没有替换actionbar时使用;
11.setSupportActionBar(mToolbar);设置toolbar替换actionbar;
12.getLayoutInflater().inflate(R.layout.view_tv,bar);Toolbar添加自定义view

二.DrawerLayout常用的方法

DrawerLayout.isDrawerOpen(Gravity.LEFT)是否开启;
DrawerLayout.openDrawer(Gravity.LEFT);开启抽屉
DrawerLayout.closeDrawer(Gravity.RIGHT);关闭抽屉

三.ToolBar和DrawerLayout代码

先去掉自带的ActionBar,在清单文件中

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

(1)布局文件代码:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/imageview"></ImageView>

   <androidx.drawerlayout.widget.DrawerLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/drawerlayout">
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical">
           <TextView
               android:layout_width="match_parent"
               android:layout_height="0dp"
               android:layout_weight="1"
               android:background="#66B036"></TextView>
           <TextView
               android:layout_width="match_parent"
               android:layout_height="0dp"
               android:layout_weight="3"></TextView>
       </LinearLayout>

       <LinearLayout
           android:layout_width="300dp"
           android:layout_height="match_parent"
           android:layout_gravity="left"
           android:orientation="vertical"
           android:background="#fff">
           <Button
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="关闭抽屉"
               android:id="@+id/close"></Button>
           <Button
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="啦啦啦"
               android:id="@+id/button1"></Button>
       </LinearLayout>
   </androidx.drawerlayout.widget.DrawerLayout>

</LinearLayout>

(2)java 代码:MainActivity.java

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerlayout;
    private Button close;
    private Button button1;
    private ImageView imageview;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        close = (Button) findViewById(R.id.close);
        button1 = (Button) findViewById(R.id.button1);
        imageview = (ImageView) findViewById(R.id.imageview);

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerlayout.closeDrawer(Gravity.LEFT);
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"啦啦啦",Toast.LENGTH_SHORT).show();
            }
        });
        imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerlayout.openDrawer(Gravity.LEFT);
            }
        });

    }
}

四.SlideMenu实现抽屉

1.SlideMenu的属性:

设置模式: setMode(SlidingMenu.LEFT);
设置触摸屏幕的模式:setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//TOUCHMODE_FULLSCREEN全屏;TOUCHMODE_MARGIN边界;TOUCHMODE_NONE不能滑动
设置左侧菜单滑动显示的内容:slidingMenu.setMenu(R.layout.slide_menu);
设置左侧滑动菜单的阴影宽度:slidingMenu.setShadowWidth(300);
设置滑动时的渐变程度:slidingMenu.setFadeDegree(0.5f);范围0.0f-1.0f
设置淡入淡出的效果:slidingMenu.setFadeEnabled(true);
设置左侧滑动菜单的阴影图片(颜色):setShadowDrawable();
设置滑出时主页面显示的剩余宽度:slidingMenu.setBehindOffset(200);

2.代码实现:

(1)导入第三方moudle:slide
1.xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="关闭"></Button>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:text="哒哒哒"></Button>

</LinearLayout>

2.MainActity中代码

public class SlideActivity extends AppCompatActivity {
    private ImageView imageview2;
    private  SlidingMenu slidingMenu ;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide);
        imageview2 = (ImageView) findViewById(R.id.imageview2);
        slidingMenu= new SlidingMenu(this);
        imageview2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                slidingMenu.showMenu();
            }
        });


        slidingMenu.setMode(SlidingMenu.LEFT);
//        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        View inflate = LayoutInflater.from(this).inflate(R.layout.layout, null);
        Button button = inflate.findViewById(R.id.button);
        Button button1 = inflate.findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                slidingMenu.showContent();
            }
        });
        slidingMenu.setMenu(inflate);
        slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
    }
}

TOOLBar

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity"
    android:orientation="vertical">
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:id="@+id/imageview3"></ImageView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="标题"
            android:id="@+id/textview"></TextView>
    </androidx.appcompat.widget.Toolbar>
    <androidx.drawerlayout.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawerlayout2">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#66B036"></TextView>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="3"></TextView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:orientation="vertical"
            android:background="#fff">
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="关闭抽屉"
                android:id="@+id/close2"></Button>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="啦啦啦"
                android:id="@+id/button2"></Button>
        </LinearLayout>
    </androidx.drawerlayout.widget.DrawerLayout>


</LinearLayout>

MainActity代码

public class Main2Activity extends AppCompatActivity {
    private ImageView imageview3;
    private TextView textview;
    private DrawerLayout drawerlayout;
    private Button close2;
    private Button button2;
    private SlidingMenu slidingMenu;






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        imageview3 = (ImageView) findViewById(R.id.imageview3);
        textview = (TextView) findViewById(R.id.textview);
        drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout2);
        close2 = (Button) findViewById(R.id.close2);
        button2 = (Button) findViewById(R.id.button2);
        slidingMenu = new SlidingMenu(this);


        imageview3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerlayout.openDrawer(Gravity.LEFT);
                close2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        drawerlayout.closeDrawer(Gravity.LEFT);
                    }
                });
                button2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(Main2Activity.this, "哒哒哒", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                slidingMenu.showMenu(SlidingMenu.RIGHT);
                View inflate = LayoutInflater.from(Main2Activity.this).inflate(R.layout.layout, null);
                Button button = inflate.findViewById(R.id.button);
                Button button2 = inflate.findViewById(R.id.button2);

                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        slidingMenu.showContent();
                    }
                });
                slidingMenu.setMenu(inflate);
                
            }
        });



    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值