如何在AlertDialog中设置EditText的边距

时间:2014-03-16 01:12:06

标签: android

我在EditText内使用AlertDialog,如:

    final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
    final EditText input = new EditText(getActivity());
    alert.setView(input);

现在我想为此EditText设置边距,因此它不会占用对话框的整个宽度。我怎样才能做到这一点? (此时我不考虑自定义Layout的选项

enter image description here

4 个答案:

答案 0 :(得分:4)

您可以在setView方法

中传递spacing参数
alert.setView(view ,left_space , top_space , right_space , bottom_space);

答案 1 :(得分:3)

如果您不想为自定义布局充气, 试试这个

AlertDialog.Builder dialog = new AlertDialog.Builder(Login.this);
final EditText email_input = new EditText(Login.this);
LinearLayout linearLayout = new LinearLayout(Login.this);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER;

email_input.setHint("Email Address");
email_input.setLayoutParams(layoutParams);

linearLayout.addView(email_input);
linearLayout.setPadding(60, 0, 60, 0);

dialog.setTitle("Forgot Password?");
dialog.setMessage("Enter Your Email Address");

dialog.setView(linearLayout);

dialog.setPositiveButton("Send", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // Some Code 
    }
});

dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        dialog.dismiss();
    }
});

dialog.show();

答案 2 :(得分:0)

在设置为setPadding (int left, int top, int right, int bottom)中的视图之前,请尝试使用EditText上的AlertDialog方法。

在某些情况下,您可以使用负填充来模拟边距。

答案 3 :(得分:0)

对于这种东西,你必须有一个自定义对话框:

以下是如何做到这一点:

第1步:创建CustomDialog.java

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;


    public class CustomDialogClass extends Dialog{

    public Activity c;
    public Dialog d;
    public Button yes, no;

    public CustomDialogClass(Activity a) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.customdialog);

    }

    }

第2步:创建自定义dialog.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2sp"
        android:background="#000000" >
    </LinearLayout>

    <TextView
        android:id="@+id/txt_dia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="@string/hello"
        android:textColor="#00BFFF"
        android:textSize="35sp"
        android:textStyle="bold" >
    </TextView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2sp"
        android:background="#000000" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#F3F3F4"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/navigationid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="10sp"
            android:text="@string/navigation"
            android:textSize="22sp"
            android:textStyle="normal" 
            android:textColor="#00BFFF"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2sp"
        android:background="#000000" >
    </LinearLayout>

</LinearLayout>

第3步:在onCreate()中的主要活动中;请致电:

final CustomDialogClass dialog = new CustomDialogClass(TutorialActivity.this);
        dialog.show();


           //This part is optional. Just to close the dialog after few seconds.
        final Timer time = new Timer();
        time.schedule(new TimerTask() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        }, 5000);

那就是......简单......:)