Android5.0系统:Material风格以及DrawerLayout抽屉效果

2024-06-10 12:08

本文主要是介绍Android5.0系统:Material风格以及DrawerLayout抽屉效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先上效果图:
抽屉关闭
抽屉打开
主要组件:

  1. DrawerLayout
  2. Toolbar

首先,配置文件代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app= "http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextView android:text="@string/hello_world" android:layout_width="wrap_content"android:layout_height="wrap_content" /><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:minHeight="?attr/actionBarSize"android:elevation="10dp"app:contentInsetLeft="72dp"app:contentInsetStart = "72dp"android:background="?attr/colorPrimary"></android.support.v7.widget.Toolbar><android.support.v4.widget.DrawerLayoutandroid:id="@+id/drawer"android:layout_below="@id/toolbar"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="10dp"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button"android:text="New Button"android:layout_marginTop="55dp"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New CheckBox"android:id="@+id/checkbox"android:layout_marginTop="51dp"/><Switchandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Switch"android:id="@+id/switch1"/><ProgressBarandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/progressBar"android:layout_marginTop="55dp"android:indeterminate="false" /></LinearLayout><LinearLayoutandroid:id="@+id/menu_frame"android:layout_gravity="left"android:layout_marginEnd="10dp"android:background="#087654"android:layout_marginRight="10dp"android:orientation="vertical"android:paddingTop="48dp"android:layout_width="match_parent"android:layout_height="match_parent"></LinearLayout></android.support.v4.widget.DrawerLayout></RelativeLayout>

使用DrawerLayout时需注意:DrawerLayout中需要放两个Layout,在收起的Layout中一定要设置:

android:layout_gravity="left"

Activity代码:

package com.demo.monty.materialdemo;import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;public class MainActivity extends AppCompatActivity {private DrawerLayout mDrawerLayout;private ActionBarDrawerToggle mDrawerToggle;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true);mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.open,R.string.close){@Overridepublic void onDrawerClosed(View drawerView) {super.onDrawerClosed(drawerView);}@Overridepublic void onDrawerOpened(View drawerView) {super.onDrawerOpened(drawerView);}};mDrawerLayout.setDrawerListener(mDrawerToggle);mDrawerToggle.syncState();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.menu_main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();//noinspection SimplifiableIfStatementif (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}
}

下面是设置Material风格的界面:

Material风格自定义
配置文件如下,styles.xml:

<resources><!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.NoActionBar"><!-- Customize your theme here. --><item name="android:colorPrimary">#675634</item><item name="android:colorPrimaryDark">#993309</item><item name="android:colorAccent">#7767ff</item><item name="android:textColorPrimary">#ffff33</item><item name="android:textColor">#3a3e55</item><item name="android:background">#3e3d38</item><item name="android:actionBarSize">30dp</item><item name="android:navigationBarColor">#3a3e55</item></style>
</resources>

其中部分属性在5.0以下操作系统中无法生效:

<item name="android:navigationBarColor">#3a3e55</item> //在5.0以下无法生效,以及华为手机Mate S 5.1系统中也无法生效,其他机器目前没有验证。

这篇关于Android5.0系统:Material风格以及DrawerLayout抽屉效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1048118

相关文章

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

Linux系统中的firewall-offline-cmd详解(收藏版)

《Linux系统中的firewall-offline-cmd详解(收藏版)》firewall-offline-cmd是firewalld的一个命令行工具,专门设计用于在没有运行firewalld服务的... 目录主要用途基本语法选项1. 状态管理2. 区域管理3. 服务管理4. 端口管理5. ICMP 阻断

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

Windows 系统下 Nginx 的配置步骤详解

《Windows系统下Nginx的配置步骤详解》Nginx是一款功能强大的软件,在互联网领域有广泛应用,简单来说,它就像一个聪明的交通指挥员,能让网站运行得更高效、更稳定,:本文主要介绍W... 目录一、为什么要用 Nginx二、Windows 系统下 Nginx 的配置步骤1. 下载 Nginx2. 解压

如何确定哪些软件是Mac系统自带的? Mac系统内置应用查看技巧

《如何确定哪些软件是Mac系统自带的?Mac系统内置应用查看技巧》如何确定哪些软件是Mac系统自带的?mac系统中有很多自带的应用,想要看看哪些是系统自带,该怎么查看呢?下面我们就来看看Mac系统内... 在MAC电脑上,可以使用以下方法来确定哪些软件是系统自带的:1.应用程序文件夹打开应用程序文件夹

windows系统上如何进行maven安装和配置方式

《windows系统上如何进行maven安装和配置方式》:本文主要介绍windows系统上如何进行maven安装和配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. Maven 简介2. maven的下载与安装2.1 下载 Maven2.2 Maven安装2.

使用Python实现Windows系统垃圾清理

《使用Python实现Windows系统垃圾清理》Windows自带的磁盘清理工具功能有限,无法深度清理各类垃圾文件,所以本文为大家介绍了如何使用Python+PyQt5开发一个Windows系统垃圾... 目录一、开发背景与工具概述1.1 为什么需要专业清理工具1.2 工具设计理念二、工具核心功能解析2.

Linux系统之stress-ng测压工具的使用

《Linux系统之stress-ng测压工具的使用》:本文主要介绍Linux系统之stress-ng测压工具的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、理论1.stress工具简介与安装2.语法及参数3.具体安装二、实验1.运行8 cpu, 4 fo

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后