Activity 对话框主题未显示为对话框
Activity with dialog theme not shown as dialog
我正在处理一个 Android 项目,我创建了一个 activity 应该使用对话框主题,但它没有正确显示。
下面是我的活动布局XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.DialogWindowTitle"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/dialog_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="@android:style/DeviceDefault.ButtonBar.AlertDialog">
<Button android:id="@+id/btnCopyToClipbard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Copy to Clipboard"
style="@android:style/Widget.DeviceDefault.Button.Borderless"/>
<Button android:id="@+id/btnClose"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Close"
style="@android:style/Widget.DeviceDefault.Button.Borderless"/>
</LinearLayout>
</LinearLayout>
下面是我的 activity 的定义方式
<activity
android:name=".CustomAlertDialogActivity"
android:theme="@style/Theme.AppCompat.Light.Dialog">
</activity>
activityclass如下
public class CustomAlertDialogActivity extends BaseActionBarActivity
{
TextView txtDialogTitle;
TextView txtDialogMessage;
Bundle bundle;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_alert_dialog);
txtDialogTitle = (TextView)findViewById(R.id.dialog_title);
txtDialogMessage = (TextView)findViewById(R.id.dialog_message);
bundle = getIntent().getExtras();
if (bundle != null)
{
txtDialogTitle.setText("An error occurred");
txtDialogMessage.setText("No error was specified");
return;
}
txtDialogTitle.setText(bundle.getString(CustomAlertDialog.DIALOG_TITLE));
txtDialogMessage.setText(bundle.getString(CustomAlertDialog.DIALOG_MESSAGE));
}
}
BaseActionBarActivity
是我自己的 class 扩展了 AppCompatActivity
下面是我的警告对话框的显示方式
如果它有任何不同,这个 activity 是在一个库项目中,该项目被包含在我的应用程序中。
你的 Activity
是这样找我的:
(分别为 Android 6.0 和 Android 4.4)
为了测试,我只是将您的代码复制到一个空项目中,如您所见,它可以正常工作(尽管它需要一些 UI 润色)。
我最好的猜测是你无意中在 super-class BaseActionBarActivity
的某处做了 setTheme()
等。因此,作为第 1 步,将 BaseActionBarActivity
替换为 AppCompatActivity
并查看是否有任何变化。
我正在处理一个 Android 项目,我创建了一个 activity 应该使用对话框主题,但它没有正确显示。
下面是我的活动布局XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.DialogWindowTitle"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/dialog_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="@android:style/DeviceDefault.ButtonBar.AlertDialog">
<Button android:id="@+id/btnCopyToClipbard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Copy to Clipboard"
style="@android:style/Widget.DeviceDefault.Button.Borderless"/>
<Button android:id="@+id/btnClose"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Close"
style="@android:style/Widget.DeviceDefault.Button.Borderless"/>
</LinearLayout>
</LinearLayout>
下面是我的 activity 的定义方式
<activity
android:name=".CustomAlertDialogActivity"
android:theme="@style/Theme.AppCompat.Light.Dialog">
</activity>
activityclass如下
public class CustomAlertDialogActivity extends BaseActionBarActivity
{
TextView txtDialogTitle;
TextView txtDialogMessage;
Bundle bundle;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_alert_dialog);
txtDialogTitle = (TextView)findViewById(R.id.dialog_title);
txtDialogMessage = (TextView)findViewById(R.id.dialog_message);
bundle = getIntent().getExtras();
if (bundle != null)
{
txtDialogTitle.setText("An error occurred");
txtDialogMessage.setText("No error was specified");
return;
}
txtDialogTitle.setText(bundle.getString(CustomAlertDialog.DIALOG_TITLE));
txtDialogMessage.setText(bundle.getString(CustomAlertDialog.DIALOG_MESSAGE));
}
}
BaseActionBarActivity
是我自己的 class 扩展了 AppCompatActivity
下面是我的警告对话框的显示方式
如果它有任何不同,这个 activity 是在一个库项目中,该项目被包含在我的应用程序中。
你的 Activity
是这样找我的:
(分别为 Android 6.0 和 Android 4.4)
为了测试,我只是将您的代码复制到一个空项目中,如您所见,它可以正常工作(尽管它需要一些 UI 润色)。
我最好的猜测是你无意中在 super-class BaseActionBarActivity
的某处做了 setTheme()
等。因此,作为第 1 步,将 BaseActionBarActivity
替换为 AppCompatActivity
并查看是否有任何变化。