使用Toolbar遇到的问题

记录在使用Toolbar过程中遇到的各种问题。
布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />
</LinearLayout>

坑一:Toolbar setTitle()方法无效
会默认显示AndroidManifest.xml中当前Activity label标签的文本,如果当前Activity 未设置label标签着向上查找Application label标签的文本,如果Application 未设置label标签,则显示包名。
解决方法:
1.在setSupportActionBar(toolbar);之前调用setTitle()方法。
2.使用getSupportActionBar().setTitle();
3.在onResume()中调用setTitle()方法

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Title");//在setSupportActionBar()前才有效
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.ic_format_list_bulleted_white_24dp);
        toolbar.setLogo(R.mipmap.ic_launcher);
        toolbar.setSubtitle("SubTitle");
    }

坑二:Toolbar被系统状态栏覆盖
在layout中添加下列标签即可

android:fitsSystemWindows="true"

坑三:最右的overflow颜色
在参考了Android Studio 创建Blank Activity的Toolbar布局文件后,修改了上面的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:toolbar="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:popupTheme="@style/AppTheme.PopupOverlay" />
    </LinearLayout>
</LinearLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值