无法将活动更改为AlertDialog

时间:2017-05-03 07:33:27

标签: android android-activity android-alertdialog

我尝试了很多主题,将活动变为警报对话框是徒劳的。我记得以前完成它但现在无法做到

这是App的主题:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/background</item>

</style>

这是活动,我正在尝试更改为警告对话框:

真实活动包含的不止于此(按钮,线性布局,文本视图)。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.famiddle.android.thechanakyanitiplus.NotificationCenter">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/notification_center_title"
    android:gravity="center_horizontal"
    android:textColor="@color/colorAccent"
    android:layout_marginTop="8dp" />

</ScrollView>

这是java文件(NotificationCenter.java)

public class NotificationCenter extends AppCompatActivity {  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // To remove status bar
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
      //      WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_notification_center);}}

将开始此活动的按钮:

 LinearLayout notificationManager = (LinearLayout) findViewById(R.id.notification_manager);
    notificationManager.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), NotificationCenter.class));

        }
    });

最后,AndroidManifest文件:

 <activity
        android:name=".NotificationCenter"
        android:theme="@style/Theme.AppCompat.Dialog"
        android:excludeFromRecents="true">

    </activity>

我尝试过这里提到的不同主题Android Activity as a dialog,但每当我点击按钮时,它就会崩溃应用程序(重新启动它)。

2 个答案:

答案 0 :(得分:0)

你见过这个link吗?

你能否详细说明你想要实现的目标。

答案 1 :(得分:0)

如果你问我,这很简单。试试这个主题并注意父主题:

Theme.AppCompat.Light.Dialog。这就是将活动转变为AlertDialog所需要的。如果你需要一个黑暗的主题,删除“光”。我还添加了一些与您的应用主题相匹配的“项目”。我希望这会有所帮助。

    <!-- NOTIFICATION MANAGER THEME -->
<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/background</item>
</style>