【问题标题】:Set default text style when using AlertDialog.Builder.setCustomTitle()使用 AlertDialog.Builder.setCustomTitle() 时设置默认文本样式
【发布时间】:2011-12-09 14:50:51
【问题描述】:

这可能很容易,但我在任何地方都找不到答案。

我将 setCustomTitle 用于我的警报对话框,以便将我自己的视图用于对话框标题:

View customTitle = getLayoutInflater().inflate(R.layout.dialog_title, null);
new AlertDialog.Builder(AddBusActivity.this).setCustomTitle(customTitle).(some more stuff).show();

还有 dialog_title.xml:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView android:text="My Title"
            android:id="@+id/title"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:gravity="right|center_vertical">
  </TextView>
</LinearLayout>

我的问题是让标题TextView 使用默认对话框标题样式。 在我的设备中,警报对话框背景是暗的,因此文本必须是亮的,但在其他设备中可能会有所不同。

我尝试将 android:textAppearance="@android:style/TextAppearance.DialogWindowTitle" 添加到 TextView,但它导致文本为黑色,而不是正确的风格。

我该如何解决这个问题?

【问题讨论】:

    标签: android android-layout android-ui android-styles


    【解决方案1】:

    我在样式和颜色方面遇到了类似问题,但通过使用自定义对话框而不是 AlertDialog 解决了它。

    1) 我的代码与

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setTitle(mName);
    ...
    mDialog = builder.create();
    mDialog.show();
    

    改为

    dialog = new Dialog(mContext);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_custom_title);
    // initializing Views 
    dialog.show();
    

    2) 在 layout.dialog_custom_title 中设置需要的样式(背景和颜色)

    <LinearLayout xmlns: android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/dialogs_background"
    android:orientation="vertical" >
    ...
    <TextView
    ...
    android:textColor="@color/TxtLightBlue" 
    android:textSize="@dimen/text_size_default"
    />
    ...
    

    希望对某人有所帮助。

    【讨论】:

    • 这值得+1 :)
    【解决方案2】:

    我使用下面的代码很好地设置了颜色和间距

     <TextView android:text="My Title"
        android:id="@+id/title"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textSize="24sp"
        android:textColor="@color/green"
        android:textStyle="bold"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:gravity="center_horizontal|center_vertical">
    </TextView>
    

    【讨论】:

      猜你喜欢
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 2011-05-29
      相关资源
      最近更新 更多