2016-09-07 118 views
0

我正在开发一个Android应用程序。在我的应用程序中,我想在对话框中显示数据。基本上,我想在单击按钮时在对话框中显示Activity。我知道如何将Activity显示为对话框。请看下面我目前如何展示活动作为对话。在Android中使用自定义对话框显示活动

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> 

所以,当我打开活动。它显示为如下的对话框。

enter image description here

正如你在截图中看到,上面代码的问题是我不能完全对设计进行控制。我的意思是我不能根据需要设置对话框的背景颜色。我将根据设备进行更改。我无法改变对话的主题。另外,我不能删除对话框的标题等等。

请看看这个

enter image description here

就像在上面的截图中,我会完全超过设计控制。我可以在标题中添加图标。我可以设置任何我想要的主题等等。

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

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

回答

1

更多的往往不是这些看似美丽的警报对话框不alertdialogs可言,但你可以控制它具有透明背景的活动。 Onclicklisteners正式放置(例如,点击透明背景将关闭活动)。因此设计这些活动比定制alertdialog更容易

+0

请如何设置透明的活动。我可以有链接吗? –

+0

(http://nullpointerbay.com/2015/08/13/android-appcompat-design-lib-activity-transparency.html)将引导您了解基础知识。还有其他相关的SO答案[这里](http://stackoverflow.com/questions/17542517/activity-with-transparent-background)和[这里](http://stackoverflow.com/questions/2176922/how-to -create-transparent-activity-in-android) –

1

你实际上在做什么是你只是在改变你的活动的主题。 达到你想要什么,最好的方法是通过创建一个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); 
    AlerdDialog alert = builder.create(); 
    alert.show(); 

这种方式,你想

0

您可以使用AlertDialog,基本上可以使用AlertDialog来显示带有OK和Cancel按钮的对话框消息。它可以用来中断并询问用户他/她的选择是否继续或停止。

的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(); 

这将以这种方式显示输出。 enter image description here

0

它最好使用AlertDialog。因为如果你按主题完成,那么对于不同版本的设备,其外观将会改变。

1

我有一个工作Activity与主题类型Dialog。它很容易定制,并且当您将登录信息放入其他课程时,代码变得更加清晰。

所以我想建议你一些东西。首先要删除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标签的其他内容可以通过您的ActivityonCreate功能轻松更改setTitle

这里是我的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> 
+0

但是主题颜色会根据设备而改变。对? –

+0

您也可以通过为父'LinearLayout'设置背景色来自定义颜色。见编辑的答案 –