android - 滚动 Activity 标题

标签 android android-activity title

我在 Activity 中动态加载标题;每当标题太长时,我希望它滚动以便可以阅读整个标题。

我已经在自定义 XML 文件和 requestFeature 中尝试过,

android:singleLine="true" 
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true" 

我试过的另一种方法

TextView textView = (TextView) findViewById(android.R.id.title);
textView.setSelected(true); 
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setMarqueeRepeatLimit(1);

textView.setFocusable(true);
textView.setFocusableInTouchMode(true);
textView.requestFocus();
textView.setSingleLine(true);

在 ellipsize() 处给了我空指针。我很茫然,真的。我怎样才能达到这个效果?

最佳答案

您的第二种方法无效,因为(TextView) findViewById(android.R.id.title) 返回 null。

我建议关注 Bhuro 的 answer ,特别是关于如何自定义标题栏。本质上,您需要一个自定义 titlebar.xml 来定义您在自定义标题栏中想要的内容(在您的情况下,只是一个 TextView)。 titlebar.xml 示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/myTitle" 
        android:text="This is my new title and it is very very long"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:singleLine="true" 
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:scrollHorizontally="true"
        android:focusable="true"
        android:focusableInTouchMode="true" 
    />
</LinearLayout>

然后你在你的 Activity 中指定它:

final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

if (customTitleSupported) {
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}

关于android - 滚动 Activity 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13341424/

相关文章:

html - 有没有办法让 title= 属性在 Chrome 上工作?

android - Apache Cordova 闪屏未在 Android 中显示

android - 9 补丁 Android 不显示边距

Android Bootstrap 和 Android studio 无法添加库

Android:从 onReceive 方法设置新的闹钟

jquery - 如何在 jQuery mmenu 中更改菜单的标题

android - 可以访问通话记录的跨平台移动框架

android - 在 Iphone 上使用数据库,就像在 android 上使用微调器一样

java - 从另一个 Activity Android studio 导入数据

ios - 在 Swift 中对 UIButton 使用 setTitle 时出现延迟