在 Android 中显示 activity 和自定义对话框

Showing activity with custom dialog in Android

我正在开发 Android 应用程序。在我的应用程序中,我想在对话框中显示数据。基本上,我想在单击按钮时在对话框中显示 Activity 。我知道如何将 Activity 显示为对话框。这是我目前将 activity 显示为对话框的方式。

我在AndroidManifest中这样设置Activity

<activity
    android:configChanges="orientation|screenSize"
    android:name=".MakeCommentActivity"
    android:label="Comment"
    android:theme="@android:style/Theme.Holo.Light.Dialog" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

所以当我打开 activity。它显示为如下对话框。

如您在屏幕截图中所见,上述代码的问题是我无法完全控制设计。我的意思是我不能随心所欲地设置对话框的背景颜色。我会根据设备进行更改。我无法更改对话框的主题。此外,我无法删除对话框的标题等。

如上图所示,我将完全控制设计。我可以在标题中添加图标。我可以设置任何我想要的主题等等。

所以我能想到的是使用带有自定义视图的警报对话框。但是如果我这样做,代码将不会干净整洁,因为所有的逻辑和代码都将放在 parent activity 中。不在不同 Activity。

所以我的问题是如何打开一个 Activity 作为可以在设计中完全自定义的对话框?如果不能完全自定义,使用带有自定义视图的警告对话框是个好主意吗?因为我会像 Facebook 那样显示评论。用户也可以发表评论。

您实际上只是在更改 activity 的主题。 实现你想要的最好方法是创建一个 custom view Dialog:

//Here you inflate your design (xml layout)
View view = getLayoutInflater().inflate(R.layout.your_view, null);
//Here you can get any component of that layout (ex: textView)
TextView yourTextView = view.findViewById(R.id.your_textview_id);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(view);
    AlertDialog alert = builder.create();
    alert.show();

这样你就可以随心所欲地控制了

这些看似漂亮的警告对话框往往根本不是警告对话框,而是具有透明背景的 activity。 Onclicklisteners 被适当放置(例如点击透明背景将关闭 activity)。因此,与自定义警报对话框相比,设计这些活动更容易

您基本上可以使用 AlertDialog android AlertDialog 可用于显示带有确定和取消按钮的对话框消息。它可用于打断并询问用户 his/her 选择继续还是停止。

Android AlertDialog由三个区域组成:标题内容区操作按钮。如果您不需要,可以留下操作按钮。

AlertDialog.Builder builder = new AlertDialog.Builder(this);  
//Setting message manually and performing action on button click 

        builder.setMessage("Do you want to close this application ?")  
            .setCancelable(false)  
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int id) {  
                finish();  
                }  
            })  
            .setNegativeButton("No", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int id) {  
                //  Action for 'NO' Button  
                dialog.cancel();  
             }  
            });  

       //Creating dialog box  
        AlertDialog alert = builder.create();  
        //Setting the title manually  
        alert.setTitle("AlertDialogExample");  
        alert.show();  

这将以这种方式显示输出。

最好使用AlertDialog。因为如果你是按主题来做的,那么对于不同版本的设备,它的外观会发生变化。

我有一个主题类型 Dialog 的工作 Activity。它很容易定制,是的,当您将登录名放在另一个 class 中时,代码会变得更清晰。

所以我想给你一些建议。首先是删除 intent-filter。我实际上不知道为什么使用 intent-filter,但无论如何,看起来不是很有必要。

<activity
    android:configChanges="orientation|screenSize"
    android:name=".MakeCommentActivity"
    android:label="Comment"
    android:theme="@android:style/Theme.Holo.Light.Dialog" >
</activity>

Activity 对话框的布局很重要。您需要将 layout_heightlayout_width 设置为 match_parent 以检查您是否可以获得自定义行为。

您提到的关于 Activity 标签的另一件事可以通过 setTitleActivityonCreate 函数轻松更改。

这是我的 DialogActivity 例如

public class DialogActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

        setTitle("My custom title");
    }
}

这里使用的布局是

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

    <View
        android:layout_width="match_parent"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:background="@android:color/black"
        android:layout_height="200dp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="@android:color/black" />

</LinearLayout>