如何为工具栏标题设置transitionName?

10

我试图在工具栏标题中使用“makeSceneTransitionAnimation”添加动画,但由于它是私有的,我无法对其设置“transitionName”。

如果有人知道如何解决这个问题或者知道其他方法,请帮助我。


请参考这里的内容:https://stackoverflow.com/questions/35324079/programmatically-add-transitionname-to-toolbar-title-android - Prabha Karan
1个回答

2

自定义标题 TextView

由于您正在使用工具栏,因此可以考虑直接创建自定义标题 TextView:

禁用默认的 ActionBar 标题:

@Override
public void onCreate(Bundle savedInstanceState) {
    // ...
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Either by calling
    getSupportActionBar().setTitle(null);
    // Or
    toolbar.setTitle(null);
    // Or
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    // ...
}

还有许多其他禁用XML中默认标题的方法,其中一些在这个问题中讨论: 在appcompat-v7中删除工具栏中的标题.

然后在Activity的布局文件中,在Toolbar内添加一个自定义标题TextView:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/AppTheme"
        app:popupTheme="@style/AppTheme.PopupOverlay">

     <TextView
            android:id="@+id/custom_title"
            android:text="@string/activity_title"
            android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:transitionName="@string/title_transition_name"/>

 </android.support.v7.widget.Toolbar>

textAppearance可以用来应用与默认ActionBar标题相同的样式。

这种方法允许您对标题视图进行更多控制,特别是如果您需要在sceneTransition中播放一些额外的TextView属性Animation,并且您想能够明确定义标题的最终外观。

查找默认标题TextView

如果您喜欢,可以通过不同的方式找到默认标题TextView:

@Override
public void onCreate(Bundle savedInstanceState) {
    // ...
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // This assumes that the title is the first child of Toolbar
    TextView titleTextView = (TextView) toolbar.getChildAt(0);
    // Or this assumes that the title is the first TextView inside of Toolbar
    for(int i = 0; i < toolbar.getChildCount(); ++i) {
        View child = toolbar.getChildAt(i);
        if(view instanceof TextView) {
            titleTextView = (TextView) view;
            break;
        }
    }
}

然后可以设置transitionName

titleTextView.setTransitionName(/* Transition name */);

在这里讨论了更多查找标题TextView的方法,获取AppCompat v7 r21中ActionBar标题TextView

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接