Android Material Design向下兼容至低版本Android SDK设备

简介: Android Material Design向下兼容至低版本Android SDK设备新版的Android Material Design增加了一些很多有趣、有意思的设计元素和风格,比如最近比较常见的Floating Action Button等等。


Android Material Design向下兼容至低版本Android SDK设备


新版的Android Material Design增加了一些很多有趣、有意思的设计元素和风格,比如最近比较常见的Floating Action Button等等。这在新版的Android L,Android 6.0中随处可见。然而Android Material Design在标准的Android低版本SDK中无法使用,比如现在常用的Floating Action Button:


还有SnackBar:



等等,在Android L上可以直接使用原生Android SDK API实现。


但是还是有第三方的开源库,帮助开发者向下兼容,将Android Material Design的优秀设计元素向下支持到低版Android设备上。
MaterialDesignLibrary就是这样的第三方开源库,其在github上的网址链接:https://github.com/navasmdc/MaterialDesignLibrary
MaterialDesignLibrary的作用和目的:“This is a library with components of Android L to you use in android 2.2”。
简言之,就是让低版本Android使用上最新的Android L中新的组件。
MaterialDesignLibrary使用方法:
(1)直接将其开发包从github上下载,然后解压导入成一个Android库即可。需要注意的是,MaterialDesignLibrary现在是一个基于gradle的库,如果是Eclipse开发者,则需要一定的转换,或者直接将解压后,目录结构MaterialDesignLibrary-master\MaterialDesignLibrary-master\MaterialDesignLibrary\MaterialDesign\src\main下的代码直接导入到Eclipse工程中作为库也可以,不过需要将该目录下的java目录名整合到Eclipse下的标准src目录,最终导入后代码和工程结构如图:


(2)到这里还没完,因为MaterialDesignLibrary向下兼容开发,作者使用了另外一个第三方库NineOldAndroids,NineOldAndroids库就是帮助一些高版本的Android代码向下兼容设计的。NineOldAndroids在github上的网址链接:https://github.com/JakeWharton/NineOldAndroids
NineOldAndroids也是一个在gradle上的项目库,如果是Eclipse开发者,则需要将其中Eclipse需要的库包分离出来,Eclipse需要的库的代码在 \MaterialDesignLibrary-master\MaterialDesignLibrary-master\MaterialDesignLibrary\MaterialDesign\src\main目录下,直接将其导入到Eclipse下作为库即可,但需要调整这个目录下的java代码到Eclipse结构下的src目录中,如图:



(3)前两步导入后,就要开始添加库引用了。需要注意的是:MaterialDesignLibrary和NineOldAndroids本身就是Android库而不是一个Android APP;而MaterialDesignLibrary又引用了NineOldAndroids。
在我们自己的代码开发中,直接添加对MaterialDesignLibrary库的引用即可。就可以像Android L那样使用Floating Action Button、Snackbar等等了。


现在把MaterialDesignLibrary库中的一个演示Floating action button的Activity:ButtonsActivity.java抽出来供参考:

package com.gc.materialdesigndemo.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;

import com.gc.materialdesigndemo.R;

public class ButtonsActivity extends Activity {

	int backgroundColor = Color.parseColor("#1E88E5");

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_buttons);
		int color = getIntent().getIntExtra("BACKGROUND", Color.BLACK);
		findViewById(R.id.buttonflat).setBackgroundColor(color);
		findViewById(R.id.button).setBackgroundColor(color);
		findViewById(R.id.buttonFloatSmall).setBackgroundColor(color);
		findViewById(R.id.buttonIcon).setBackgroundColor(color);
		findViewById(R.id.buttonFloat).setBackgroundColor(color);
	}

}

ButtonsActivity.java的布局文件activity_buttons.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF" >

    <com.gc.materialdesign.views.ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <!-- FLAT BUTTON -->

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="32dp"
                android:layout_marginLeft="24dp" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Flat Button" />

                <View
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:layout_alignParentBottom="true"
                    android:background="#1E88E5" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="72dp" >

                <com.gc.materialdesign.views.ButtonFlat
                    android:id="@+id/buttonflat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="Button"
                    android:textColor="#ffffff" />
            </RelativeLayout>
            <!-- RECTANGLE BUTTON -->

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="32dp"
                android:layout_marginLeft="24dp" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Rectangle Button" />

                <View
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:layout_alignParentBottom="true"
                    android:background="#1E88E5" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="72dp" >

                <com.gc.materialdesign.views.ButtonRectangle
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="#1E88E5"
                    android:text="Button" />
            </RelativeLayout>
            <!-- SMALL FLOAT BUTTON -->

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="32dp"
                android:layout_marginLeft="24dp" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Small Float Button" />

                <View
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:layout_alignParentBottom="true"
                    android:background="#1E88E5" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="72dp" >

                <com.gc.materialdesign.views.ButtonFloatSmall
                    android:id="@+id/buttonFloatSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="#1E88E5"
                    materialdesign:iconDrawable="@drawable/ic_action_new" />
            </RelativeLayout>

            <!-- FLOAT BUTTON -->

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="32dp"
                android:layout_marginLeft="24dp" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Icon Button" />

                <View
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:layout_alignParentBottom="true"
                    android:background="#1E88E5" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="72dp" >

                <com.gc.materialdesign.views.ButtonIcon
                    android:id="@+id/buttonIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="#1E88E5"
                    materialdesign:iconDrawable="@drawable/ic_next" />
            </RelativeLayout>

            <!-- FLOAT BUTTON -->

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="32dp"
                android:layout_marginLeft="24dp" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Float Button" />

                <View
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:layout_alignParentBottom="true"
                    android:background="#1E88E5" />
            </RelativeLayout>
        </LinearLayout>
    </com.gc.materialdesign.views.ScrollView>

    <com.gc.materialdesign.views.ButtonFloat
        android:id="@+id/buttonFloat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="24dp"
        android:background="#1E88E5"
        materialdesign:animate="true"
        materialdesign:iconDrawable="@drawable/ic_action_new" />

</RelativeLayout>

其运行效果图就是本文中的第1图显示的那样。


我把全部的代码工程(MaterialDesignLibrary库,NineOldAndroids库,以及一个测试MaterialDesignLibrary的项目MaterialDesignActivity)上传到CSDN上供下载使用,CSDN下载页面:http://download.csdn.net/detail/zhangphil/9124325
将这个压缩文件下载后逐个导入,正确添加库引用后,就可以直接跑MaterialDesignActivity查看运行结果了。


相关文章
|
5天前
|
JavaScript Java Maven
云效产品使用常见问题之android sdk 构建出aar后,上传到私有maven仓库失败如何解决
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
2月前
|
Android开发
如何用Airtest脚本无线连接Android设备?
如何用Airtest脚本无线连接Android设备?
|
3月前
|
Android开发 Python
Python封装ADB获取Android设备wifi地址的方法
Python封装ADB获取Android设备wifi地址的方法
61 0
|
3月前
|
安全 开发工具 Android开发
几个Flutter常见诊断错误与解决Android toolchain - develop for Android devices X Unable to locate Android SDK
几个Flutter常见诊断错误与解决Android toolchain - develop for Android devices X Unable to locate Android SDK
287 0
|
4天前
|
Java Android开发
Android Mediatek 应用层重置USB设备功能
Android Mediatek 应用层重置USB设备功能
11 0
|
4天前
|
Android开发
Android Mediatek USB 核心驱动中增加设备 PID/VID 检查
Android Mediatek USB 核心驱动中增加设备 PID/VID 检查
3 0
|
13天前
|
编解码 人工智能 测试技术
安卓适配性策略:确保应用在不同设备上的兼容性
【4月更文挑战第13天】本文探讨了提升安卓应用兼容性的策略,包括理解平台碎片化、设计响应式UI(使用dp单位,考虑横竖屏)、利用Android SDK的兼容工具(支持库、资源限定符)、编写兼容性代码(运行时权限、设备特性检查)以及优化性能以适应低端设备。适配性是安卓开发的关键,通过这些方法可确保应用在多样化设备上提供一致体验。未来,自动化测试和AI将助力应对设备碎片化挑战。
|
1月前
|
Shell 开发工具 Android开发
ADB 下载、安装及使用教程:让你更好地管理 Android 设备
ADB 下载、安装及使用教程:让你更好地管理 Android 设备
512 2
|
1月前
|
存储 运维 网络协议
【开源物联网平台】物联网设备上云提供开箱即用接入SDK
IOTDeviceSDK是物联网平台提供的设备端软件开发工具包,可简化开发过程,实现设备快速接入各大物联网平台。设备厂商获取SDK后,根据需要选择相应功能进行移植,即可快速集成IOTDeviceSDK,实现设备的接入。
121 0
|
3月前
|
开发工具 Android开发
Android平台RTMP推送|轻量级RTSP服务|GB28181设备接入模块之实时快照保存JPG还是PNG?
Android平台RTMP推送|轻量级RTSP服务|GB28181设备接入模块之实时快照保存JPG还是PNG?