我的风格
<style name="myAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowTitleBackgroundStyle">@color/colorPrimary</item>
</style>
我的AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext(), R.style.myAlertDialog);
builder.setTitle("Dialog Title");
builder.setMessage("This is message!");
builder.create().show();
我的自定义样式完全无效。有帮助吗?
[在网上搜索了很多有关此问题的信息后,我发现windowTitleBackgroundStyle
不起作用,但是windowTitleStyle
起作用,但是结果不是我想要的:
<style name="myAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowTitleStyle">@style/myDialogTitle</item>
</style>
<style name="myDialogTitle" parent="TextAppearance.AppCompat.Title">
<item name="android:background">@color/colorPrimary</item>
</style>
[整个AlertDialog
布局中似乎有固定的填充。除此之外,getResource().getIdentifier()
和findViewById()
方法目前还无法正常工作。因此,setCustomTitle()
成为修改AlertDialog
标题的唯一方法。
1。为AlertDialog创建自定义标题布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light"
android:orientation="vertical"
android:paddingStart="24dp" //following Material Design guideline
android:paddingTop="16dp"
android:paddingEnd="24dp"
android:paddingBottom="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Marvel Cinematic Universe"
android:textAppearance="@style/TextAppearance.AppCompat.Title" //key point 1, using AlertDialog default text style
android:textColor="@android:color/white" />
</LinearLayout>
2。使用“ setCustomTitle()”将自定义标题布局应用于AlertDialogAlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
View titleView = getLayoutInflater().inflate(R.layout.dialog_title, null);
builder.setCustomTitle(titleView); //key point 2
builder.setMessage(R.string.main_marvel_info);
builder.setPositiveButton("OK", null);
builder.create().show();