Big*_*dad 97 android android-appcompat android-actionbar drawertoggle android-5.0-lollipop
所以现在Android 5.0发布了,我想知道如何实现动画操作栏图标.
这个库在这里实现它很好但是因为appcompat v7库有它如何实现它?
该库在themes.xml中引用它
<item name="drawerArrowStyle">@style/Widget.AppCompat.DrawerArrowToggle</item>
Run Code Online (Sandbox Code Playgroud)
在这种风格下
<style name="Base.V7.Theme.AppCompat" parent="Platform.AppCompat">
Run Code Online (Sandbox Code Playgroud)
UPDATE
我使用v7 DrawerToggle实现了这个功能.但是我无法设计它.请帮忙
我在v7 styles_base.xml中找到了它的样式
<style name="Base.Widget.AppCompat.DrawerArrowToggle" parent="">
<item name="color">?android:attr/textColorSecondary</item>
<item name="thickness">2dp</item>
<item name="barSize">18dp</item>
<item name="gapBetweenBars">3dp</item>
<item name="topBottomBarArrowSize">11.31dp</item>
<item name="middleBarArrowSize">16dp</item>
<item name="drawableSize">24dp</item>
<item name="spinBars">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我把它添加到我的样式中并且没有用.也添加到我的attr.xml
<declare-styleable name="DrawerArrowToggle">
<!-- The drawing color for the bars -->
<attr name="color" format="color"/>
<!-- Whether bars should rotate or not during transition -->
<attr name="spinBars" format="boolean"/>
<!-- The total size of the drawable -->
<attr name="drawableSize" format="dimension"/>
<!-- The max gap between the bars when they are parallel to each other -->
<attr name="gapBetweenBars" format="dimension"/>
<!-- The size of the top and bottom bars when they merge to the middle bar to form an arrow -->
<attr name="topBottomBarArrowSize" format="dimension"/>
<!-- The size of the middle bar when top and bottom bars merge into middle bar to form an arrow -->
<attr name="middleBarArrowSize" format="dimension"/>
<!-- The size of the bars when they are parallel to each other -->
<attr name="barSize" format="dimension"/>
<!-- The thickness (stroke size) for the bar paint -->
<attr name="thickness" format="dimension"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
但是崩溃并且在这样做时说颜色类型错误.我错过了什么?
小智 242
首先,您应该知道现在android.support.v4.app.ActionBarDrawerToggle
已弃用.
你必须用它替换它android.support.v7.app.ActionBarDrawerToggle
.
这是我的例子,我使用new Toolbar
来替换ActionBar
.
MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawerLayout, mToolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close
);
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle.syncState();
}
Run Code Online (Sandbox Code Playgroud)
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)
您可以在AndroidDocument#DrawerArrowToggle_spinBars上阅读文档
此属性是实现菜单到箭头动画的关键.
public static int DrawerArrowToggle_spinBars
转换期间条是否应该旋转
必须是布尔值,"true"或"false".
所以,你设置这个:<item name="spinBars">true</item>
.
然后可以呈现动画.
希望这可以帮到你.
ian*_*ake 24
如果您按照创建导航抽屉培训中的建议使用提供的DrawerLayout支持库,则可以使用新添加的android.support.V7 .app.ActionBarDrawerToggle(注:从现在反对的不同.android.support V4 .app.ActionBarDrawerToggle):
抽屉关闭时显示汉堡图标,抽屉打开时显示箭头.当抽屉打开时,它会在这两种状态之间激活.
虽然培训尚未更新以考虑弃用/新类,但您应该能够使用几乎完全相同的代码 - 实现它的唯一区别是构造函数.
tim*_*tim 17
我创建了一个具有类似功能的小应用程序
主要活动
public class MyActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.open,
R.string.close
)
{
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
invalidateOptionsMenu();
syncState();
}
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
syncState();
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//Set the custom toolbar
if (toolbar != null){
setSupportActionBar(toolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
actionBarDrawerToggle.syncState();
}
}
Run Code Online (Sandbox Code Playgroud)
我的那个Activity的XML
<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"
tools:context=".MyActivity"
android:id="@+id/drawer"
>
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/toolbar_custom"/>
</FrameLayout>
<!-- The navigation drawer -->
<ListView
android:layout_marginTop="?attr/actionBarSize"
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#457C50"/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
我的自定义工具栏XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:background="?attr/colorPrimaryDark">
<TextView android:text="U titel"
android:textAppearance="@android:style/TextAppearance.Theme"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)
我的主题风格
<resources>
<style name="AppTheme" parent="Base.Theme.AppCompat"/>
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDarker</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
</style>
<color name="primary">#457C50</color>
<color name="primaryDarker">#580C0C</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
我的样式值 - v21
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
要回答问题的更新部分:要设置抽屉图标/箭头的样式,您有两种选择:
为此,请drawerArrowStyle
在主题中覆盖,如下所示:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<item name="drawerArrowStyle">@style/MyTheme.DrawerArrowToggle</item>
</style>
<style name="MyTheme.DrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="color">@android:color/holo_purple</item>
<!-- ^ this will make the icon purple -->
</style>
Run Code Online (Sandbox Code Playgroud)
这可能不是你想要的,因为ActionBar本身应该与箭头具有一致的样式,所以,最有可能的是,你想要选项二:
使用您自己的主题(您可能应该从中派生出来)覆盖全局应用程序主题的android:actionBarTheme
(actionBarTheme
for appcompat)属性,ThemeOverlay.Material.ActionBar/ThemeOverlay.AppCompat.ActionBar
如下所示:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<item name="actionBarTheme">@style/MyTheme.ActionBar</item>
</style>
<style name="MyTheme.ActionBar" parent="ThemeOverlay.AppCompat.ActionBar">
<item name="android:textColorPrimary">@android:color/white</item>
<!-- ^ this will make text and arrow white -->
<!-- you can also override drawerArrowStyle here -->
</style>
Run Code Online (Sandbox Code Playgroud)
这里一个重要的注意事项是,当使用自定义布局Toolbar
而不是库存ActionBar实现时(例如,如果您使用DrawerLayout
- NavigationView
- Toolbar
组合来实现在半透明状态栏下可见的材质样式抽屉效果),该actionBarTheme
属性显然不是自动拾取(因为它意味着由AppCompatActivity
默认值处理ActionBar
),因此对于您的自定义,Toolbar
请不要忘记手动应用您的主题:
<!--inside your custom layout with DrawerLayout
and NavigationView or whatever -->
<android.support.v7.widget.Toolbar
...
app:theme="?actionBarTheme">
Run Code Online (Sandbox Code Playgroud)
- ThemeOverlay.AppCompat.ActionBar
如果在派生主题中设置属性,这将解析为AppCompat的默认值或覆盖.
PS关于drawerArrowStyle
覆盖和spinBars
属性的一点评论- 许多消息来源建议应该设置true
为获取抽屉/箭头动画.spinBars
事实上true
,默认情况下,它在AppCompat中(检查Base.Widget.AppCompat.DrawerArrowToggle.Common
样式),您根本不必重写actionBarTheme
以使动画正常工作.即使您覆盖它并将属性设置为动态,也可以获得动画false
,它只是一个不同的,不那么圆润的动画.这里重要的是使用ActionBarDrawerToggle
,它是花哨的动画drawable的东西.
归档时间: |
|
查看次数: |
104772 次 |
最近记录: |