我在 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/