使用AppCompat_v7 21.0.0d的几个兼容问题

1.实现新的ActionBarDrawerToggle动画



ActionBarDrawerToggle使用最新的AppCompat_v7 21会出现一个非常帅的动画。使用方式在Androidstudio以下先加入compile

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.0'
}

然后直接将ActionBarDrawerToggle的impot使用import android.support.v7.app.ActionBarDrawerToggle;

之后的构造方法就能够使用

 mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name);

当然须要一个ToolBar。


追加:原以为须要ToolBar的构造方法才有那个动画,如今发现原来仅仅要引用了android.support.v7.app.ActionBarDrawerToggle。三个构造方法都带这个非常帅的动画

 public ActionBarDrawerToggle(android.app.Activity activity, android.support.v4.widget.DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes) { /* compiled code */ }

    public ActionBarDrawerToggle(android.app.Activity activity, android.support.v4.widget.DrawerLayout drawerLayout, android.support.v7.widget.Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) { /* compiled code */ }

    <T extends android.graphics.drawable.Drawable & android.support.v7.app.ActionBarDrawerToggle.DrawerToggle> ActionBarDrawerToggle(android.app.Activity activity, android.support.v7.widget.Toolbar toolbar, android.support.v4.widget.DrawerLayout drawerLayout, T slider, int openDrawerContentDescRes, int closeDrawerContentDescRes) { /* compiled code */ }

2.使用ToolBar取代ActionBar

Android L加入了一个新控件来一步步的替换ActionBar,ToolBar有着更灵活的扩展。全然可以取代ActionBar,而且有着自己作为一个View的灵活性。

仅仅是有点不方便的是。ToolBar须要在每一个Activity中声明,无论是在XML中或者代码

  • 改动主题
<resources>
    <style name="AppTheme" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>

        <!-- Actionbar color -->
        <item name="colorPrimary">@color/accent_material_dark</item>
        <!--Status bar color-->
        <item name="colorPrimaryDark">@color/accent_material_light</item>
        <!--Window color-->
        <item name="android:windowBackground">@color/dim_foreground_material_dark</item>
    </style>
</resources>
假设你原来使用ActionBarPullToRefresh控件这个时候会发现。进度条和底边有俩个dp的间隔。假设使用了ToolBar,那么你就能够控制ActionBar的高度,当然你能够改动ActionBarPullToRefresh源代码来解决问题
  • 布局中加入ToolBar
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        ></android.support.v7.widget.Toolbar>

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

这样就得到了一个取代ActionBar的Toolbar,測试发现内部的Fragment getActionBar还是能够直接使用,由于在Activiyty中将ToolBar设置为了ActionBar,所以内部的Fragment 对ActionBar的操作全然能够和曾经一样使用。

当然也能够在fragment中设置自己的Toolbar。


3.Actionbar的ListModel错位问题
ActionBar.NAVIGATION_MODE_LIST在替换了AppCompat 之后在4.x上显示会出现错位的情况,而且显示这种方法已经被废弃。例如以下图

解决方式是使用TintSpinner取代Spinner,奇怪的是TintSpinner在官网居然查不到相关的信息,当然它也在android.support.v7.internal.widget包中。打开看源代码发现
public class TintSpinner extends android.widget.Spinner {
    private static final int[] TINT_ATTRS;

    public TintSpinner(android.content.Context context) { /* compiled code */ }

    public TintSpinner(android.content.Context context, android.util.AttributeSet attrs) { /* compiled code */ }

    public TintSpinner(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr) { /* compiled code */ }
}

他继承Spinner,可是没有做不论什么的修改,好奇观,替换了之后确实攻克了问题。

追加:sb了,源代码是有的,仅仅是没有关联到as,而是放在:AndroidStudio_0_8_14+SDK ▸ AndroidSDK ▸ sources ▸ android-21 ▸ android ▸ support ▸ v7 ▸ internal ▸ widget>TintSpinner.java.源代码例如以下
public class TintSpinner extends Spinner {

    private static final int[] TINT_ATTRS = {
            android.R.attr.background,
            android.R.attr.popupBackground
    };

    public TintSpinner(Context context) {
        this(context, null);
    }

    public TintSpinner(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.spinnerStyle);
    }

    public TintSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, TINT_ATTRS,
                defStyleAttr, 0);
        setBackgroundDrawable(a.getDrawable(0));

        if (Build.VERSION.SDK_INT >= 16 && a.hasValue(1)) {
            setPopupBackgroundDrawable(a.getDrawable(1));
        }

        a.recycle();
    }

}





4.待续。。。








posted @ 2016-01-01 12:53  mengfanrong  阅读(395)  评论(0编辑  收藏  举报