android个人笔记之 toolbar 动画

1、效果图,网上找的,主要动画细节是一样的


2、集成appcompat_v7 包就成比如:

compile 'com.android.support:appcompat-v7:25.0.1'
3、布局上面一个toolbar,下面一个drawlayout布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
    android:id="@+id/tool001"
    android:layout_width="match_parent"
    android:layout_height="44dp"
   >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="测试001"/>
    </RelativeLayout>
</android.support.v7.widget.Toolbar>
<include
    layout="@layout/activity_main"
    ></include>
</LinearLayout>
4、drawlayout布局,主界面一个图片,抽屉是一个随便的布局
<?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:id="@+id/drawerLayout001"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="hnq.sanhenxian.MainActivity">
     <ImageView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:src="@mipmap/ic_launcher"/>
     <include
         layout="@layout/left"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_gravity="start"
         >
     </include>
</android.support.v4.widget.DrawerLayout>
5.抽屉的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
    android:background="@color/colorAccent">
    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:gravity="center_horizontal"
        android:text="目录1"/>
    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:gravity="center_horizontal"
        android:text="目录2"/>
    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:gravity="center_horizontal"
        android:text="目录3"/>
    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:gravity="center_horizontal"
        android:text="目录4"/>

    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:gravity="center_horizontal"
        android:text="目录5"/>

</LinearLayout>
6、然后得让主题编程noactionbar的如下面
<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>

7、下面就是主要代码了
package hnq.sanhenxian;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {
    private ActionBarDrawerToggle mDrawerToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toolbar toolbar= (Toolbar) findViewById(R.id.tool001);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        DrawerLayout drawerLayout= (DrawerLayout) findViewById(R.id.drawerLayout001);
        mDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.ss,R.string.ss1);
        mDrawerToggle.syncState();
        drawerLayout.addDrawerListener(mDrawerToggle);
    }
}
主要是
ActionBarDrawerToggle
这个东西再起作用,构造函数后面的2个是字符串资源,这里随便写2个就行了,比如我是这样的
<resources>
    <string name="app_name">SanHenXian</string>
    <string name="ss">打开</string>
    <string name="ss1">关闭</string>
</resources>

感觉这2个参数没用,当然可能其他地方有用,可是标示什么的。按源码的解释,是标示打开关闭的描述。
实现原理:
根据path画出来的。具体怎么画的看不懂,没得算法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio 中,Toolbar 是一个 Android 应用常用的组件,可以用来放置应用的标题、菜单、操作按钮等,通常放置在应用的顶部。 Toolbar 是在 Android 5.0 (API 级别 21) 中引入的,它是由一个 ViewGroup 和多个子视图组成的,包括标题、操作按钮和菜单项等。使用 Toolbar 可以提供更大的自由度和灵活性,因为它可以定制化自己的外观和行为。 在 Android Studio 中,可以通过向布局文件添加 Toolbar 控件来实现 Toolbar,也可以通过代码来设置 Toolbar 的属性和添加菜单项。可以通过设置 Toolbar 的外观和行为来满足应用的需求。 以下是一个简单的示例,展示了如何在 Android Studio 中创建 Toolbar: 1. 在布局文件中添加 Toolbar 控件: ```xml <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" app:titleTextColor="@android:color/white" app:title="@string/app_name" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> ``` 2. 在 Activity 中设置 Toolbar: ```java Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); ``` 3. 在 onCreateOptionsMenu() 方法中添加菜单项: ```java @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } ``` 上述代码演示了如何创建一个简单的 Toolbar,可以根据应用的需求进行定制化设置和添加菜单项等功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值