如何在工具栏标题的相同位置添加TextView? [英] How to add TextView in same location of title in Toolbar?

查看:87
本文介绍了如何在工具栏标题的相同位置添加TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的xml文件中有一个工具栏,其中没有定义子视图.我想在调用setSupportActionBar(toolbar)时在工具栏中默认标题的确切位置上添加一个TextView.对于特定事件,在发生特定事件时,我喜欢淡入或淡出这两种视图.

I have a toolbar in my xml file, with no child views defined in it. I want to add a TextView on exact location of default title in toolbar when we call setSupportActionBar(toolbar). On particular events, I like to fade in or out these two views in case of particular events.

我尝试在活动的onCreate()中执行以下操作,但是除了工具栏中的默认标题之外,什么都看不到.

I tried to do the following in onCreate() of the activity, but nothing is visible except the default title in toolbar.

    TextView tv = new TextView(this);
    tv.setLayoutParams(toolbarTitle.getLayoutParams());
    tv.setHeight(toolbarTitle.getHeight());
    tv.setWidth(toolbarTitle.getWidth());
    tv.setText("Other Title");
    tv.setLeft(toolbarTitle.getLeft());
    toolbar.addView(tv);

我还尝试在工具栏内添加一个RelativeLayout,其宽度和高度设置为与父项匹配,但是这一次,工具栏的默认标题TextView消失了.

I also tried to add a RelativeLayout inside the toolbar with width and height set to match parent, but this time, default title TextView of toolbar disappears.

如何在工具栏中默认标题TextView的确切位置上添加另一个TextView?

How can add another TextView on exact location of default title TextView in toolbar?

推荐答案

我在活动的onCreate()方法中使用getSupportActionBar().setDisplayShowTitleEnabled(false)禁用工具栏上的标题,并将两个重叠的textView封装在一起,并在布局中的工具栏内使用relativeLayout封装它们.然后,根据需要为它们设置动画.更改文本时,我使用的是titleTextView.setText()而不是toolbar.setTitle()方法.

I disable title in toolbar using getSupportActionBar().setDisplayShowTitleEnabled(false) in onCreate() method of my activity and put two overlapping textViews encapsulating them with relativeLayout inside toolbar in layout. Then I animate them as I want. While changing text, I used titleTextView.setText() rather then toolbar.setTitle() method.

这篇关于如何在工具栏标题的相同位置添加TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆