2012-07-26 59 views
2

可能重复:
Show AlertDialog in any position of the screen如何定义的视图中的警告对话框的位置

我提出申请我在其中显示alert dialog。我想用一个custom layout我警报对话框。如何根据我的需要在view/layout中设置其位置。

我的代码:

LayoutInflater inflater=getLayoutInflater(); 
View view = inflater.inflate(R.layout.actionbarmain,null); 
view.setMinimumWidth(200); 
view.setMinimumHeight(400); 

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); 
alertDialog.setView(view); 
alertDialog.show(); 
+1

看看[这里](http://stackoverflow.com/questions/5469005/show -alertdialog-in-screen-any-position-of-the-screen)问题和接受的答案。 – thegrinner 2012-07-26 11:53:12

+0

编译器在getWindow()和requestWindowFeature(Window.FEATURE_NO_TITLE)中显示问题; – 2012-07-26 11:57:16

+0

将您的代码发布到您希望在您的活动的'onCreate()'中调用的地方。 – Abhilasha 2012-07-26 11:58:11

回答

0

与你需要的布局创建活动,并指定主题,在明显的对话框。

这是一个例子:你需要根据你的REQ去改变它

public class EditPropertyActivity extends Activity { 

    /** Called when the activity is first created. */ 
    // @Override 
    /* 
    * (non-Javadoc) 
    * 
    * @see android.app.Activity#onCreate(android.os.Bundle) 
    */ 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.dialog); 
     setTitle(""); 
     TextView propName = (TextView) findViewById(R.id.tv_propName);   

     propName.setText(getIntent().getExtras().getString("prop_label")); 

     final EditText ed_propValue = (EditText) findViewById(R.id.ed_propValue); 
     ed_propValue.setText(value); 
     if (tagName.equals("timeout")) { 
      ed_propValue.setInputType(InputType.TYPE_CLASS_NUMBER); 
     } 
     Button btn_save = (Button) findViewById(R.id.btn_save); 
     btn_save.setOnClickListener(new View.OnClickListener() { 
     //onclick 
     } 
    } 

在清单:

 <activity 
      android:name="EditPropertyActivity" 
      android:theme="@android:style/Theme.Dialog" > 
     </activity> 

布局dialog.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tv_propName" 
     android:layout_width="200dip" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginBottom="10dip" 
     android:layout_marginLeft="15dip" 
     android:layout_marginRight="25dip" 
     android:layout_marginTop="10dip" 
     android:text="Medium Text" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <EditText 
     android:id="@+id/ed_propValue" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="2dp" 
     android:layout_marginRight="2dp" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/btn_save" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/lbl_btn_save" /> 

     <Button 
      android:id="@+id/btn_cancel" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/cancel" /> 
    </LinearLayout> 

</LinearLayout> 
+0

谢谢你的努力夫人,但这不是我的问题的解决方案。我一次又一次地得到同样的问题 – 2012-07-26 12:13:56

+0

好吧,然后感谢您的努力 – 2012-07-26 12:18:41

0

添加自定义在警报对话框中的布局,你必须在其中充气布局:

testxml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

      <Button 
       android:id="@+id/btn1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="test" /> 
      <Button 
       android:id="@+id/btn2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="test" /> 
      <Button 
       android:id="@+id/btn3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="test" /> 
</LinearLayout> 

如果你想在比警告对话框夸大此xml:

   AlertDialog.Builder builder=new Builder(YourActivityName.this); 
       LayoutInflater inflater=getLayoutInflater(); 
       View view=inflater.inflate(R.layout.testxml, null); 
       builder.setView(view); 
       builder.setTitle("New Project"); 
       builder.setMessage("knknknknknmknknknk"); 
       builder.setPositiveButton("ok", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 

        } 
       }); 
       builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 

        } 
       }); 
       builder.create(); 
       builder.show(); 
+0

我刚刚采取线性布局有3个按钮的方向垂直相同,你可以添加尽可能多的按钮,它可以改变方向根据您的requirements.i实施这可以清除您的问题并将此布局设置为AlertDialog中的视图。 – AkashG 2012-07-26 12:35:00