4

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

我将 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,但它导致文本为黑色,这不是正确的样式。

我该如何解决这个问题?

4

2 回答 2

2

我在样式和颜色方面遇到了类似的问题,但通过使用自定义对话框而不是 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"
/>
...

希望它会帮助某人。

于 2013-04-03T12:01:27.180 回答
0

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

 <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>
于 2015-09-28T01:59:01.327 回答