【问题标题】:ActionBar title is truncated to 1 characterActionBar 标题被截断为 1 个字符
【发布时间】:2017-03-12 22:26:58
【问题描述】:

我正在使用托管ListFragment 的活动。从 ListFragment 我可以打开一个DetailTabsFragment。这两个选项卡包含两个片段:DetailFragmentReviewFragment

我在DetailFragment中设置了标题,代码如下:

((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("my title");

但正如您在屏幕截图中看到的那样,标题在第一个字符处被截断。这是为什么呢?

DetailTabsFragment的xml布局:

    <android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                            xmlns:tools="http://schemas.android.com/tools"
                                            xmlns:app="http://schemas.android.com/apk/res-auto"
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
    app:elevation="0dp">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"/>

</android.support.design.widget.AppBarLayout>

一个mainActivity的布局:

<android.support.design.widget.CoordinatorLayout
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.dcs.shows.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

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

【问题讨论】:

标签: android android-actionbar material-design android-toolbar


【解决方案1】:

我遇到了同样的问题。我的架构有点不同,但标题被截断为单个字母 (T...) 的最终结果是相同的。我也玩弄了关于其他 SO 问题的每一个建议。最终,在活动中设置标题似乎是某种奇怪的比赛条件。我发现只需覆盖活动的 setTitle 函数并将对 super 的调用包装在 postDelay 可运行文件中即可为我修复它:

@Override
public void setTitle(final CharSequence title) {
    toolBar.postDelayed(new Runnable() {
        @Override
        public void run() {
            MainActivity.super.setTitle(title);
        }
    }, 100);
}

我使用工具栏的 postDelayed 作为一种方便的方法。但是你当然可以在这里使用处理程序。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-01
    • 2017-09-05
    • 2011-09-07
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    相关资源
    最近更新 更多