代码之家  ›  专栏  ›  技术社区  ›  Blundell

安卓:如何创建一个滚动标题的对话框?

  •  2
  • Blundell  · 技术社区  · 14 年前

    好的,所以我已经阅读了和开发人员网站上的自定义对话框说明。 http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

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

    基本上,我的标题太长了,我希望它能滚动(像textview),或者更好的话,仍然有一个我认为叫做“字幕”的效果。

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

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

    3 回复  |  直到 11 年前
        1
  •  6
  •   Thorstenvv    14 年前

    自定义窗口(以及对话框)标题可以通过请求窗口功能“自定义”标题来完成,这必须在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
    

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

    布局/自定义标题.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>
    

    由于字幕功能的某些限制,文本视图必须可调焦,并且只有在调焦时才会滚动(最初应该是这样)。

        2
  •  7
  •   liquid_code    13 年前

    可以使对话框标题多行:

    TextView title = (TextView) dialog.findViewById(android.R.id.title);
    title.setSingleLine(false);
    
        3
  •  0
  •   max.mustermann    11 年前

    我认为Ruslank(用于获取文本视图)和Thorstenvv(用于使文本视图可滚动)的组合回答是最佳实践。