Android: How to create a Dialog with a Scrolling title?

时间秒杀一切 提交于 2020-01-30 06:40:04

问题


Ok so I've read the Custom Dialog explanation on the And Dev website http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

It show's you how to make a custom dialog, but not how to customise the title!

Basically my title is too long and I want it to scroll (like textview) or better still have a 'marquee' effect i think it's called.

Or if I can't make it scroll, give it more space to wrap onto more lines!

Any ideas, I don't hold out much hope as it's not on android.dev :-(


回答1:


Customizig window (and thus also dialog) titles can be done by requesting the window feature CUSTOM_TITLE, which must be done before setContentView.

So in your Dialog / Activity subclasses onCreate(), call the following:

super.onCreate(savedInstance);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // <- insert this

Then, after your setContentView, do this:

setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); // <- insert this

The layout can generally contain anything you want. For a marquee text control. e.g. do this:

layout/custom_title.xml:

<FrameLayout android:id="@+id/FrameLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <TextView android:id="@+id/caption_text" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:text="This is a very very long text that will not fit into a caption regularly, so it will be displayed using marquee..." 
        android:lines="1"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:scrollHorizontally="true" 
        android:marqueeRepeatLimit="marquee_forever" 
        android:ellipsize="marquee"
        ></TextView>
</FrameLayout>

Due to some constraints with the marquee feature, the text view has to be made focusable and it will only be scrolling when focused (which it initially should be).




回答2:


You can make dialog title multiline:

TextView title = (TextView) dialog.findViewById(android.R.id.title);
title.setSingleLine(false);



回答3:


I consider a combination of RuslanK's (for getting the TextView) Thorstenvv's (for making TextView scrollable) answer to be best practice.



来源:https://stackoverflow.com/questions/3578929/android-how-to-create-a-dialog-with-a-scrolling-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!