【问题标题】:Android: How to create a Dialog with a Scrolling title?Android:如何创建带有滚动标题的对话框?
【发布时间】:2010-08-26 19:50:54
【问题描述】:

好的,我已经阅读了 And Dev 网站上的自定义对话框说明 http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

它向您展示了如何制作自定义对话框,而不是如何自定义标题!

基本上我的标题太长了,我希望它可以滚动(如 textview),或者更好的是仍然有一个“选框”效果,我认为它被称为。

或者如果我不能让它滚动,给它更多的空间来换行!

任何想法,我都不抱太大希望,因为它不在 android.dev 上 :-(

【问题讨论】:

    标签: android xml dialog title


    【解决方案1】:

    您可以将对话框标题设为多行:

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

    【讨论】:

    • 我认为这个答案比在不需要时将对话框子类化要好一些。
    【解决方案2】:

    自定义窗口(以及对话框)标题可以通过请求窗口功能 CUSTOM_TITLE 来完成,这必须在 setContentView 之前完成。

    因此,在您的 Dialog / Activity 子类 onCreate() 中,调用以下代码:

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

    然后,您的 setContentView 之后,执行以下操作:

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

    布局通常可以包含您想要的任何内容。 对于选取框文本控件。例如这样做:

    布局/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>
    

    由于选取框功能的一些限制,文本视图必须具有焦点,并且只有在获得焦点时才会滚动(最初应该如此)。

    【讨论】:

    • 如果我必须使用 setTitle(myString);在我的活动中??
    【解决方案3】:

    我认为结合使用 RuslanK(用于获取 TextView)和 Thorstenvv(用于使 TextView 可滚动)的答案是最佳实践。

    【讨论】:

      猜你喜欢
      • 2011-02-08
      • 2021-06-20
      • 2016-03-29
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多