具有可更改的单行标题的Android对话框

问题描述

| 简而言之,我正在使用\“ DurationPickerDialog \”进行工作,该工作原理类似于DatePickerDialog的工作原理,但是基于xsd:duration类型工作,因此用户指定了年数,月数,天数等。 我还实现了“模糊持续时间”功能,该功能为我提供持续时间,例如“一个月前”。我真的很希望能够以与更新DatePickerDialog \标题相同的方式来更新DurationPickerDialog \标题,但是似乎存在问题。在DatePickerDialog中,他们始终将其设置为一行,这样就不会出现“跳动”的情况。这是Android来源如何处理DatePickerDialog的标题
// Note: before the skim-readers look at this bit,realize that this is NOT my
// code but Android\'s internal code for the DatePickerDialog.
@Override
public void show() {
    super.show();

    /* Sometimes the full month is displayed causing the title
     * to be very long,in those cases ensure it doesn\'t wrap to
     * 2 lines (as that looks jumpy) and ensure we ellipsize the end.
     */
    TextView title = (TextView) findViewById(R.id.alertTitle);
    title.setSingleLine();
    title.setEllipsize(TruncateAt.END);
}
不幸的是,我无法访问他们的
R.id.alertTitle
,因为它是
com.android.internal.R
的一部分。 我已经看到了类似stackoverflow的实现,在该实现中,我需要修改
Window.FEATURE_CUSTOM_TITLE
属性,但这似乎并不能(很容易)让我修改标题。 还有另一篇stackoverflow帖子,提到了如何在运行时在两个不同的XML布局之间更改标题,但这似乎也没有太大帮助,因为标题应在每次更改持续时间时进行修改,并且为每个持续时间创建XML布局显然不是一个好主意。 因此,既然他们通过访问我们凡人无法获得的价值“作弊”,那么我还有另一种方法可以做到吗? 编辑:通过一些黑魔法,似乎它现在像我想要的那样将文本省略了吗?只是以前不是,现在我似乎无法重现该问题。因此,我想我们正在研究的同时,有人可以向我解释我如何实现这一魔术吗?     

解决方法

        我将通过扩展
Dialog
类并为其创建自定义xml布局来实现自定义对话框。 您需要一些用于加号/减号和顶部/底部组合的自定义按钮背景,以及一些按钮侦听器来操纵这些值。 由于要使用持续时间值,因此您可能将需要比现有对话框所提供的空间更多的空间。 这样,您可以将标题设置为任何您喜欢的标题。让我知道您是否需要代码示例。 例: 对话框类:
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class DurationDialog extends Dialog {

    private Button yearButtonPlus;
    private Button yearButtonMinus;
    private TextView dialogBody;
    private TextView dialogTitle;

    private String dialogBodyString;
    private String dialogTitleString;


    public DurationDialog(final Context context,String dialogBody,String dialogTitle) {
        super(context,R.style.CustomDialogTheme);
        this.dialogBodyString = dialogBody;
        this.dialogTitleString = dialogTitle;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.my_dialog);

        yearButtonPlus = (Button) findViewById(R.id.dialog_year_button_plus);
        yearButtonMinus = (Button) findViewById(R.id.dialog_year_button_minus);
        dialogBody = (TextView) findViewById(R.id.dialog_body);
        dialogTitle = (TextView) findViewById(R.id.dialog_title);

        dialogBody.setText(dialogBodyString);
        dialogTitle.setText(dialogTitleString);


        yearButtonPlus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //do year increment here
            }
        });

        //etc...
    }

}
在您的活动中,调用
this.showDialog(DURATION_DIALOG);
// DURATION_DIALOG只是在活动顶部指定的整数,以标识下一个代码段的对话框,该代码可以实际创建对话框:
import android.app.Activity;
import android.app.Dialog;

public class MyActivity extends Activity  {

    private Dialog dialog;


    //Lots of other activity stuff...



    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case DURATION_DIALOG:
                dialog = new DurationDialog(Activity.this,\"your title\",\"your body\");
                dialog.setOnDismissListener(onDismissListener);
                break;
            default:
                dialog = null;
        }
        return dialog;
    }

}

//put this listener in as an inner class of MyActivity:
    private DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {

        DurationDialog dialog = (DurationDialog) dialog;
        //grab the duration stuff out of your dialog and do stuff with it....

    }
};
最后,您可以像在
styles.xml
文件中一样设置对话框主题。例如:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<resources>
    <style name=\"CustomDialogTheme\" parent=\"@android:style/Theme.Dialog\">
        <item name=\"android:windowBackground\">@drawable/bgnd_transparent</item>
        <item name=\"android:windowIsFloating\">true</item>
    </style>
</resources>
祝好运!     ,        如果要在不使用自定义视图的情况下修改对话框标题的TextView(更改文本,样式,行为...),请执行以下操作:
TextView tv = (TextView) dialog.findViewById(android.R.id.title);
tv.setText(\"New title\");
...