AppCompat_v7 21.0.0d compatibility issues, appcompatv7

Source: Internet
Author: User

AppCompat_v7 21.0.0d compatibility issues, appcompatv7

1. Implement the New ActionBarDrawerToggle Animation



ActionBarDrawerToggle uses the latest AppCompat_v7 21 to display a handsome animation. To use it, add compile under Androidstudio.

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

Then, import android. support. v7.app. ActionBarDrawerToggle to the impot of ActionBarDrawerToggle;

The subsequent constructor can be used.

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

Of course, a ToolBar is required.


Append: We thought we needed the ToolBar constructor to have the animation. Now we can see that we only need to reference android. support. v7.app. ActionBarDrawerToggle. All three constructor methods contain this handsome animation.

 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. Use ToolBar instead of ActionBar

Android L adds a new control to replace the ActionBar step by step. ToolBar has more flexible extensions, can replace ActionBar completely, and has its own flexibility as a View. It is a little inconvenient. The ToolBar needs to be declared in every Activity, whether in XML or code.

  • Modify topic
<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>
If you use the ActionBarPullToRefresh control, you will find that there are two dp intervals between the progress bar and the bottom side. If ToolBar is used, you can control the height of the ActionBar, of course, you can modify the ActionBarPullToRefresh source code to solve this problem.
  • Add ToolBar to layout
    <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);

In this way, a Toolbar instead of ActionBar is obtained. The test shows that the internal Fragment getActionBar can still be used directly, because the ToolBar is set to ActionBar in Activiyty, therefore, the internal Fragment operation on the ActionBar can be used as before. You can also set your own Toolbar in fragment.
3. The ListModel mis-location problem of Actionbar. NAVIGATION_MODE_LIST after AppCompat is replaced, the system displays mis-location on 4.x and shows that this method has been deprecated, as shown in figure
The solution is to use TintSpinner instead of Spinner. The strange thing is that TintSpinner cannot find relevant information on the official website. Of course, it is also in the android. support. v7.internal. widget package.
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 */ }}

He inherited the Spinner, but did not make any changes. It was amazing. After the replacement, the problem was solved.
Append: sb. The source code is available, but it is not associated with the as, but placed in: androidStudio_0_8_14 + SDK release AndroidSDK release sources plugin android-21 plugin android plugin support release v7 plugin internal plugin widget> TintSpinner. java. the source code is as follows:
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. To be continued...






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.