Apply Android Material Design Tips

一、在xml文件中,showAsAction属性报错
因为showAsAction是在API 11中引入的属性,在使用support库时,应当使用自定义前缀,而不是使用android作为前缀。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/menu_compose"
        android:icon="@drawable/header_icon_compose_normal"
        app:showAsAction="ifRoom"
        android:title="@string/menu_action_compose" />
</menu> 

二、使用ToolBar时定义的属性

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

app:theme指定Dark版本的ActionBar
app:popupTheme指定overflow菜单弹出的样式

三、 给Buttons添加ripple效果
一般的Button样式如下:
/drawable/button.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"  android:drawable="@drawable/button_pressed"/>
    <item android:drawable="@drawable/button_normal"/>
</selector> 

想要在Lollipop中使用Ripple效果,可以在drawable-21文件夹下添加
/drawable/button.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:drawable="@drawable/button_normal" />
</ripple> 

对于可点击的Views,也可以添加Ripple效果
在xml文件中可以做如下修改:

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground" /> 

也可以在代码中修改:

int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
int backgroundResource = typedArray.getResourceId(0, 0);
view.setBackgroundResource(backgroundResource); 

上面的Ripple效果都是在View所在的空间内显示,想要使ripple效果超过view的边界,可以使用?attr/selectableItemBackgroundBorderless,对于大视图中的ImageButtons和samller Buttons效果特别好。

四、 在Lollipop版本中,对话框中按钮的文字和进度条默认使用系统的colorAccent属性(青色)
可以在AppTheme中修改android:alertDialogTheme的属性为自定义的效果
themes.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:alertDialogTheme">@style/AlertDialogCustom</item>
</style> 

styles.xml

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/primary</item>
</style> 

对于弹出对话框的删除按钮,也可以更改其效果来实现强调作用。
自定义AlertDialog主题主题,复写android:buttonBarPositiveButtonStyle属性
styles.xml

<style name="AlertDialogCustom.Destructive">
    <item name="android:buttonBarPositiveButtonStyle">@style/DestructiveButton</item>
</style>

<style name="DestructiveButton"
    parent="android:Widget.DeviceDefault.Button.Borderless">
    <item name="android:textColor">@color/red</item>
</style> 

在代码中,指定上述theme即可

AlertDialog alertDialog = new AlertDialog.Builder(
getActivity(),
R.style.AlertDialogCustom_Destructive)
.setPositiveButton(R.string.button_delete, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Delete Action
}
})
.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Cancel Action
}
})
.setTitle(R.string.title_delete_item)
.create(); 

Material Dialogs on Every Platorm
下面是实现上面效果的一些库地址:

https://github.com/afollestad/material-dialogs
https://github.com/fengdai/AlertDialogPro/
https://github.com/drakeet/MaterialDialog

五、改变文本的颜色
可以使用ColorStateList
color/menu_text.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/primary_dark" android:state_activated="true" />
    <item android:color="@color/primary_text" />
</selector> 

在layout文件中

<TextView
android:id="@+id/menu_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Text.Title"
android:textColor="@color/menu_selector" /> 

六、改变图标的颜色
在Android Lollipop中,利用tint属性,使用ColorStateList即可。

也可以使用下面的代码动态设置

iconView.setColorFilter(getResources().getColor(R.color.primary_dark));

下面是Stack Overflow上面的方法http://stackoverflow.com/questions/11095222/android-imageview-change-tint-to-simulate-button-click/18724834#18724834这里写链接内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值