安卓开发toolbar设置logo_Android ToolBar

组成部分

导航,左边第一个

logo,左边第二个

标题和子标题

自定义View

action menu

基本用法

ToolBar可以独立实现所有功能,不需要调用setSupportActionBar

导航

logo

toolbar.setLogo(R.drawable.ic_launcher);

标题和子标题

toolbar.setTitle("Title");

toolbar.setSubtitle("Subtitle");

自定义View

自定义View会在标题与action menu之间,如果导航、logo和标题都没有话,自定义View的空间就很大了。相对与导航栏,我更喜欢这样的设计。

代码

TextView textView = new TextView(this);

textView.setText("自定义View");

toolbar.addView(textView);

XML

与LinearLayout等一样,直接放在里面就行

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="自定义View" />

action menu

toolbar.inflateMenu(R.menu.menu_main);

// 要在setSupportActionBar之后调用

toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

@Override

public boolean onMenuItemClick(MenuItem menuItem) {

switch (menuItem.getItemId()) {

case R.id.action_settings:

break;

}

return true;

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在CollapsingToolbarLayout中显示工具栏的标志,图标,标题和副标题,你需要在布局文件中使用CollapsingToolbarLayout和Toolbar。 在Toolbar设置logo和标题,你可以使用以下代码: ```xml <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:logo="@drawable/logo" android:title="Title"/> ``` 要在CollapsingToolbarLayout中显示标题和副标题,你可以使用以下代码: ```xml <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin"/> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@drawable/background_image" app:layout_collapseMode="parallax"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Title" app:layout_collapseMode="pin"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Subtitle" app:layout_collapseMode="pin"/> </android.support.design.widget.CollapsingToolbarLayout> ``` 在这个例子中,ImageView使用了parallax属性,允许图像在CollapsingToolbarLayout滚动时以不同的速度滚动。 注意:要使用CollapsingToolbarLayout和Toolbar,你需要在你的build.gradle文件中添加以下依赖项: ```gradle implementation 'com.android.support:design:28.0.0' ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值