如何在标题中将标题居中?

时间:2018-08-15 15:51:02

标签: android android-layout android-studio

我需要将标题放在progressDialog中,我正在尝试使用样式

  <style name="progressDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>
    <item name="android:textColor">@color/popup_main_title</item>
    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:textAlignment">center</item>
    <item name="android:textColorSecondary">@color/popup_main_title</item>
    <item name="android:textColorPrimary">@color/popup_main_title</item>
</style>


 progressDialog = new ProgressDialog(context, R.style.progressDialogTheme);
        progressDialog.setTitle(context.getResources().getString(R.string.firmware_downloading_firmware_title));
        progressDialog.setMessage(context.getResources().getString(R.string.firmware_downloading_firmware_message));

3 个答案:

答案 0 :(得分:1)

设置自定义标题:

    progressDialog = new ProgressDialog(context, R.style.progressDialogTheme);
    TextView tvTitle = new TextView(context);
    tvTitle.setGravity(Gravity.CENTER);
    tvTitle.setText(context.getResources().getString(R.string.firmware_downloading_firmware_title));
    progressDialog.setCustomTitle(tvTitle);
    progressDialog.setMessage(context.getResources().getString(R.string.firmware_downloading_firmware_message));

当然,您可以更改标题TextView的其他属性,例如文本颜色和背景颜色。

答案 1 :(得分:0)

尝试一下:

<style name="progressDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>
    <item name="android:textColor">@color/popup_main_title</item>
    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:textAlignment">center</item>
    <item name="android:textColorSecondary">@color/popup_main_title</item>
    <item name="android:textColorPrimary">@color/popup_main_title</item>
    <item name="android:windowTitleStyle">@style/DialogTitle</item>
</style>

<style name="DialogTitle" parent="@android:style/TextAppearance.DialogWindowTitle">
    <item name="android:gravity">center_horizontal</item>
</style>

答案 2 :(得分:0)

ProgressDialog已过时,因此您可以使用AlertDialog,因为ProgressDialog请参考以下ProgressDialog代码。每当显示进度对话框时,都需要调用此函数。

代码:

    public void setProgressDialog() {

    int llPadding = 30;
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    ll.setPadding(llPadding, llPadding, llPadding, llPadding);
    ll.setGravity(Gravity.CENTER);
    LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    llParam.gravity = Gravity.CENTER;
    ll.setLayoutParams(llParam);

    ProgressBar progressBar = new ProgressBar(this);
    progressBar.setIndeterminate(true);
    progressBar.setPadding(0, 0, llPadding, 0);
    progressBar.setLayoutParams(llParam);

    llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    llParam.gravity = Gravity.CENTER;
    TextView tvText = new TextView(this);
    tvText.setText("Loading ...");
    tvText.setTextColor(Color.parseColor("#000000"));
    tvText.setTextSize(20);
    tvText.setLayoutParams(llParam);

    ll.addView(progressBar);
    ll.addView(tvText);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setView(ll);

    AlertDialog dialog = builder.create();
    dialog.show();
    Window window = dialog.getWindow();
    if (window != null) {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.copyFrom(dialog.getWindow().getAttributes());
        layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(layoutParams);
    }
}

输出:

enter image description here