android5.0自带兼容控件__Toolbar

Toolbar


本文由 Luzhuo 编写,请尊重个人劳动成果,转发请保留该信息.
原文: http://blog.csdn.net/Rozol/article/details/50189449
微博: http://weibo.com/u/2524456400


  • ToolBar是android5.0当中用于代替ActionBar使用,从而实现Material Design风格.
  • 本文只讲ToolBar向下兼容部分,也就是低版本api可用使用的部分.
  • 库导入: v7包(compile 'com.android.support:appcompat-v7:24.2.1')

基本使用

XML布局:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#03A9F4" />

Activity代码:

public class ToolBarDemo extends AppCompatActivity {
    private Toolbar toolbar;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toolbar);
        initView();
        initData();
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
    }

    private void initData() {
        setSupportActionBar(toolbar);
    }
}

主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

效果:

源码研究

XML属性研究:

<declare-styleable name="Toolbar">
    <attr name="titleTextAppearance" format="reference" />      // 标题文字样式
    <attr name="subtitleTextAppearance" format="reference" />   // 副标题文字样式
    <attr name="title" />                                       // 设置标题文字
    <attr name="subtitle" />                                    // 设置副标题文字

默认

    <attr name="android:gravity" />
    <attr name="titleMargins" format="dimension" />             // 标题:四周边距+垂直居中(超出范围不显示)

titleMargins

    <attr name="titleMarginStart" format="dimension" />         // 标题:左边距

titleMarginStart

    <attr name="titleMarginEnd" format="dimension" />
    <attr name="titleMarginTop" format="dimension" />           // 标题:上边距

titleMarginStart

    <attr name="titleMarginBottom" format="dimension" />
    <attr name="contentInsetStart" />                           // 内容:左边距[0无边距]

titleMarginStart

    <attr name="contentInsetEnd" />
    <attr name="contentInsetLeft" />                            // 内容:XXX未知XXX
    <attr name="contentInsetRight" />
    <attr name="maxButtonHeight" format="dimension" />          // XXX未知XXX

    <attr name="collapseIcon" format="reference" />             // XXX未知XXX
    <attr name="collapseContentDescription" format="string" />
    <attr name="popupTheme" />
    <attr name="navigationIcon" format="reference" />           // 图标
    <attr name="navigationContentDescription" format="string" />// 图标描述(不会显示)
    <attr name="android:minHeight" />

    <attr name="logo" />                                        // Logo
    <attr name="logoDescription" format="string" />             // Logo描述

titleMarginStart

    <attr name="titleTextColor" format="color" />               // 标题文字颜色
    <attr name="subtitleTextColor" format="color" />            // 副标题文字颜色
</declare-styleable>

案例:

附:

Material Design Color

Color palette:

原文






案例源代码:

GitHub

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值