2012-07-04 77 views
0

我们创建了一个自定义对话框。在android中设置自定义对话框的位置?

我们如何在中心位置的Middle处设置自定义对话框。

示例代码dialog.getWindow().setGravity(Gravity.CENTER);

现在,自定义对话框是在Top中心位置的显示。

在此先感谢。

+0

安置自己的代码在这里 –

回答

1

TestAActivity.java

包com.TestA;

import android.app.Activity; 
    import android.app.Dialog; 
    import android.content.Context; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.ImageView; 
    import android.widget.TextView; 

    public class TestAActivity extends Activity { 
     private Button btn; 

     /** Called when the activity is first created. */ 
     // TwitPicResponse tpResponse = null; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      btn = (Button) findViewById(R.id.btn1); 
      btn.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        show_Dialog(); 
       } 
      }); 

     } 

     protected void show_Dialog() { 
      // TODO Auto-generated method stub 
      //Context mContext = getApplicationContext(); 
      Dialog dialog = new Dialog(TestAActivity.this); 

      dialog.setContentView(R.layout.test); 
      dialog.setTitle("Custom Dialog"); 

      TextView text = (TextView) dialog.findViewById(R.id.text); 
      text.setText("Hello, this is a custom dialog!"); 
      ImageView image = (ImageView) dialog.findViewById(R.id.image); 
      image.setImageResource(R.drawable.ic_launcher); 

      dialog.show(); 
     } 
    } 

enter code here 

的test.xml

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/layout_root" 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:padding="10dp" 
       > 
    <ImageView android:id="@+id/image" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_marginRight="10dp" 
       /> 
    <TextView android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:textColor="#FFF" 
       /> 
</LinearLayout> 

main.xml中

<?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" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

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

</LinearLayout> 
+0

你在说什么? – Sekar

+0

这是自定义对话框的完整演示代码。如果您使用此答案,请纠正此问题。 – KeTaN

相关问题