不同版本的适配及Material Design

关于适配有很多种方案:
1.在代码中根据不同的版本,通过if语句判断,不同的版本调用不同的api

2.如果确定某一个方法只会在某一个版本及以上的Android设备上被调用则使用TargetApi注解

3.新建不同的文件夹如drawable-v21、layout-v22、values-v21,然后在里面写不同的文件如style.xml 或者activity.xml ,这样Android设备会根据当前运行的系统版本自动读取相应文件夹下的文件。

在实际工作中,我们应该根据不同的情形使用不同的适配方案。

下面就用第三条方案,提供一个MaterialDesign适配的例子:

布局很简单 就是一个RecyclerView的item里面放置了一个含有CardView的小布局,布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    app:cardCornerRadius="10dp"
    android:clickable="true">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:gravity="center_vertical"
        android:background="@drawable/foreground">

        <ImageView
            android:id="@+id/iv_img"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:scaleType="centerInside"/>
        <Button
            android:id="@+id/btn_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:layout_marginLeft="50dp"
            android:padding="20dp"
            android:elevation="20dp"
            android:background="@drawable/foreground"/>
    </LinearLayout>

</android.support.v7.widget.CardView>

因为CardView只有设置foreground点击才有波纹的效果,但是如果设置了android:foreground="@drawable/foreground"后运行在Android5.0以下的设备上视图只能看到前景色,看不见内容。因此这里在LinearLayout和Button上面设置了android:background="@drawable/foreground"

其中foreground.xml在drawable和drawable-v21下定义了不同的内容。
drawable文件夹下的foreground.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <color android:color="#ff00ff"/>
        <!--<shape>
            <solid android:color="#ff00ff"/>
        </shape>-->
    </item>

    <item >
        <color android:color="#ffffff"/>
        <!--<shape>
            <solid android:color="#ffffff"/>
        </shape>-->
    </item>


</selector>

drawable-v21文件夹下的foreground.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<!--<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item android:id="@id/mask">
        <color android:color="@color/white" />
    </item>
</ripple>-->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ff00ff"> <!--这里是扩散水波纹的色值-->
    <!--如果不写下面三行,波纹的效果会向边界外延伸;加上如下的id(id名字不能变)并不会真正绘制相应的颜色或者背景,只是告诉系统需要绘制的边界-->

    <!--如果不写这个id或者叫其他的id名字 那么不管后面你设置的是透明色还是透明图片 点击时候的波纹都没有效果-->
    <item android:id="@android:id/mask">
        <color android:color="#ffff00" />
    </item>
</ripple>

同时为了练习下Material Design材料主题的使用,我们在Android 5.0及Android 5.0以上设备、Android 5.0以下的设备使用了不同的主题。下面是具体做的事情:

在menifest.xml文件中为application定义主题android:theme="@style/AppTheme"

新建values-v21文件夹,在values和values-v21文件夹下的style.xml都定义了AppTheme这一主题

values文件夹下的style.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light">
         Customize your theme here.
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

values-v21文件夹下的style.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:ThemeOverlay.Material.Light">
         Customize your theme here.

        <!--toolbar颜色-->
        <item name="android:colorPrimary">@color/colorPrimary</item>

        <!--toolbar颜色 文字颜色-->
        <item name="android:textColorPrimary">@color/textColorPrimary</item>

        <!--状态栏颜色(?)  statusBarColor会继承该颜色 但是如果不设置statusBarColor状态栏颜色仍然没有效果-->
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>

        <!--状态栏颜色-->
        <item name="android:statusBarColor">@color/statusBarColor</item>

        <!--许多控件在选中状态或获取焦点状态下使用这个颜色
        常见有:CheckBox:checked 状态  RadioButton:checked 状态
        SwitchCompat:checked 状态  EditText:获取焦点时的 underline 和 cursor 颜色
        TextInputLayout:悬浮 label 字体颜色 等等-->
        <item name="android:colorAccent">@color/colorAccent</item>

        <!--所有可点击 Views 触摸状态下的 Ripple(涟漪)效果。仅作用于 Lollipop 及更高版本。-->
        <item name="android:colorControlHighlight">@color/colorControlHighlight</item>

        <!--Button normal 状态下的背景色。注意,这种设置与 Button 的 android:background 属性改变背景色不同的是,
        前者在 Lollipop 及更高版本上会让 Button 依旧保持阴影和 Ripple 触摸效果。-->
        <item name="android:colorButtonNormal">@color/colorButtonNormal</item>
    </style>

</resources>

将程序分别在Android 5.0以上和以下的设备上运行,得到如下结果:

Android 5.0以上(第二个按钮为点击态 有波纹显示)
这里写图片描述

Android 5.0以上(第二个按钮为点击态 没有波纹显示)
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值