带动画的ActionBar --------- ToolBar(兼容低版本)

效果:



首先工程需要引用 v7的


修改values/style.xml

<resources>

    <style name="AppBaseTheme" parent="android:Theme.Light"></style>
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppTheme.Base">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <!-- 修改style -->
    <style name="AppTheme.Base" parent="Theme.AppCompat">
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
        <!-- Actionbar color -->
        <item name="colorPrimary">@color/link_text_material_light</item>
        <!-- Status bar color -->
        <item name="colorPrimaryDark">@color/link_text_material_light</item>
        <!-- Window color -->
        <item name="android:windowBackground">@color/dim_foreground_material_dark</item>
        <!-- 將新加入的風格 AppTheme.MyDrawerStyle 設定給 drawerArrowStyle 這個屬性 -->
        <item name="drawerArrowStyle">@style/AppTheme.MyDrawerArrowStyle</item>
    </style>

    <!-- 加入一個新的 navigation drarwer 的風格 -->
    <style name="AppTheme.MyDrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">

        <!-- 將 spinBars 屬性設定為 false -->
        <item name="spinBars">false</item>
        <!-- 設定 drawer arrow 的顏色 -->
        <item name="color">@android:color/black</item>
    </style>

</resources>


现在布局中可以加入ToolBar了

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize" >
    </android.support.v7.widget.Toolbar>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:fitsSystemWindows="true" >

        <!-- Content -->

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="主界面"
                android:textColor="#000"
                android:textSize="30sp" />
        </RelativeLayout>

        <!-- Side Drawer -->

        <LinearLayout
            android:id="@+id/drawer_view"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@drawable/a"
            android:fitsSystemWindows="true"
            android:orientation="vertical" >
        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>


onCreate方法, 注意继承 ActionBarActivity

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

		// 设置ToolBar
		Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
		// Title
		toolbar.setTitle("Toolbar的标题");
		// App Logo
		// toolbar.setLogo(R.drawable.ic_launcher);
		// Sub Title
//		toolbar.setSubtitle("Sub title");

		if (toolbar != null) {
			setSupportActionBar(toolbar);
		}

		// Navigation Icon 要設定在 setSupoortActionBar 才有作用
		// 否則會出現 back button
		toolbar.setNavigationIcon(R.drawable.perm_group_system_tools);

		// 打開 up button
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
		// 實作 drawer toggle 並放入 toolbar
		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open,
				R.string.drawer_close);
		mDrawerToggle.syncState();
		mDrawerLayout.setDrawerListener(mDrawerToggle);

	}


github :https://github.com/18236887539/ToolBar

欢迎star





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值