質問

I created a custom dialog but I got an unexpected result.

The final appearance of the dialog is:

enter image description here

As you can see, my custom dialog is shown with a kind of padding in both top and bottom of the dialog, and I don't code it to do this...

The code is the next:

@Override

protected Dialog onCreateDialog(int id){
LayoutInflater  inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View newAsig = inflater.inflate(R.layout.nuevaasignatura_act, null);

//Also, down here I tried to shape the dialog by setting a functional resource, but
    //the stroke should be blue, not grey.

    RelativeLayout re = (RelativeLayout)newAsig.findViewById(R.id.layoutCrearAsig1);
    re.setBackgroundResource(R.drawable.borde);

    Typeface tf = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");

    ImageView ico = (ImageView)newAsig.findViewById(R.id.ivicono);
    ico.setBackgroundResource(R.drawable.colorprincipal);

    LinearLayout ly = (LinearLayout)newAsig.findViewById(R.id.layoutCrearAsig2);
    ly.setBackgroundColor(Color.rgb(214, 217, 224));

    TextView txt = (TextView)newAsig.findViewById(R.id.textviewnuevaasig);
    txt.setTypeface(tf);

    TextView txt1 = (TextView)newAsig.findViewById(R.id.textViewCarpeta);
    txt1.setTypeface(tf);
    txt1.setBackgroundResource(R.drawable.colorprincipal);

    Button btn = (Button)newAsig.findViewById(R.id.buttonCrearAsig);
    btn.setTypeface(tf);

    switch (id){
    case 0:
        return new AlertDialog.Builder(this)
        .setView(newAsig).create();
    }return null;

And the XML file...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/layoutCrearAsig1">

<ImageView
    android:id="@+id/ivicono"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:paddingLeft="4dp"
    android:background="@color/DarkGray"
    android:contentDescription="Icono ClassMarks"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:src="@drawable/icono_cm" />

 <TextView
     android:id="@+id/textViewCarpeta"
     android:layout_width="match_parent"
     android:layout_height="50dp"
     android:layout_toRightOf="@id/ivicono"
     android:background="@color/DarkGray"
     android:clickable="true"
     android:gravity="center"
     android:text="Carpeta"
     android:textAppearance="?android:attr/textAppearanceLarge" />

     <LinearLayout 
         android:id="@+id/layoutCrearAsig2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_below="@id/ivicono"
         android:orientation="vertical">

         <TextView
             android:id="@+id/textviewnuevaasig"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
             android:layout_marginTop="20dp"
             android:text="Crear nueva asignatura"
             android:textSize="20sp" />

         <EditText
             android:id="@+id/edittextAsig"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:ems="10"
             android:layout_gravity="center_horizontal"
             android:layout_marginTop="20dp" >
         </EditText>

         <Button 
             android:id="@+id/buttonCrearAsig"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
             android:layout_marginTop="20dp"
             android:layout_marginBottom="20dp"
             android:paddingLeft="10dp"
             android:paddingRight="10dp"
             android:textSize="20sp"
             android:text="Crear"/>

     </LinearLayout>

</RelativeLayout>

I hope you can help me. Thanks!

役に立ちましたか?

解決

The idea is AlertDialog always has 5dp top and bottom padding.

Try something like this:

AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(newAsig, 0, 0, 0, 0);

他のヒント

Try this validation.

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        builder = new AlertDialog.Builder(getActivity());
        builder.setInverseBackgroundForced(true);
    } else {
        builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top