【问题标题】:How to center the title in the dialog?如何使对话框中的标题居中?
【发布时间】:2017-04-18 17:06:16
【问题描述】:

我已经在我的应用程序中实现了对话框。但对话框中的标题默认在左侧。如何使对话框标题居中?

这是我的代码

final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.contact_query);
dialog.setTitle("Query Form");

【问题讨论】:

标签: android dialog


【解决方案1】:

你可以试试这个:

// Creating the AlertDialog with a custom xml layout (you can still use the default Android version)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.viewname, null);
builder.setView(view);

TextView title = new TextView(this);
// You Can Customise your Title here 
title.setText("Custom Centered Title");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);

builder.setCustomTitle(title);

【讨论】:

    【解决方案2】:

    *试试这个*

        Dialog dialog = new Dialog(this);
    
        dialog.setContentView(R.layout.yourlayout);
    
        TextView titleView = (TextView)dialog.findViewById(android.R.id.title);
    
        titleView.setGravity(Gravity.CENTER);
    

    更多信息 http://kodecenter.com/article?id=2390ec63-63d7-4534-a76f-cc3b10497a2c

    【讨论】:

      【解决方案3】:

      如果您使用 AlertDialog 构建器,请为您的对话框标题创建一个布局(例如,您可以在其中将文本居中),将其膨胀并将其提供给构建器。像这样:

      res/layout/dialog_title.xml:

      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark_rc"
        android:textSize="22sp"
        android:gravity="center"
        android:textColor="@color/textColorPrimaryDark"
        android:padding="10dp"/>
      

      Java:

      public Dialog onCreateDialog(Bundle savedInstanceState) {
      
          AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(act);
      
          LayoutInflater layoutInflater = act.getLayoutInflater();
          View customView = layoutInflater.inflate(R.layout.l_df_episode_details, null);
          alertDialogBuilder.setView(customView);
      
          TextView tv = (TextView) layoutInflater.inflate(R.layout.dialog_title, null);
          tv.setText("YOUR TITLE");
          alertDialogBuilder.setCustomTitle(tv); 
      
          ......
      
          alertDialog = alertDialogBuilder.create();
          return alertDialog;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-07
        • 2011-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多