2012-03-19 82 views
1

我創建了一個alertDialog來請求登錄數據。wrap_content,fill_parent

我想用所有的橫向空間,但我不能得到它:

AlertDialog.Builder loginData = new AlertDialog.Builder(activity); 
    loginData.setTitle("Tus datos"); 
    LinearLayout layout = new LinearLayout(activity); 
    layout.setOrientation(LinearLayout.VERTICAL); 
    layout.setLayoutParams(new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT)); 

    LinearLayout linea1 = new LinearLayout(activity); 
    linea1.setOrientation(LinearLayout.HORIZONTAL); 

    linea1.setLayoutParams(new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.FILL_PARENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT)); 

    final TextView Usuario = new TextView(activity); 
    Usuario.setText("Usuario"); 
    linea1.addView(Usuario); 

    final EditText usuario = new EditText(activity); 
    linea1.addView(usuario); 

    layout.addView(linea1); 
    // Set an EditText view to get user input 

    LinearLayout linea2 = new LinearLayout(activity); 
    linea2.setOrientation(LinearLayout.HORIZONTAL); 

    linea2.setLayoutParams(new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.FILL_PARENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT)); 

    final TextView Contrasena = new TextView(activity); 
      Contrasena.setText("Contraseña"); 
      linea2.addView(Contrasena); 

    final EditText contrasena = new EditText(activity); 
      linea2.addView(contrasena); 

    layout.addView(linea2); 

    loginData.setView(layout); 

    loginData.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

        try{ 
         //do the work 
        } catch (Exception e){ 
         e.printStackTrace(); 
        } 

       } 
      }); 

    loginData.show(); 
} 

我該怎麼辦呢?

回答

0

嘗試:

 ..... 
    usuario.setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.FILL_PARENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT)); 
    ..... 
    contrasena.setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.FILL_PARENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT)); 
    ..... 

但在我看來 - 你應該使用XML來佈局。

dialog.xml:

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

<TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="1" 
     > 
    <TableRow> 
     <TextView 
       android:id="@+id/label1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Usuario" 
       /> 
     <EditText 
       android:id="@+id/text1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 
    </TableRow> 
    <TableRow> 
     <TextView 
       android:id="@+id/label2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Contraseña" 
       /> 
     <EditText 
       android:id="@+id/text2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 
    </TableRow> 
</TableLayout> 

在活動:

.... 
    LayoutInflater inf = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    View layout = inf.inflate(R.layout.dialog, null); 

    AlertDialog.Builder loginData = new AlertDialog.Builder(this); 
    loginData.setTitle("Tus datos"); 
    loginData.setView(layout); 
    loginData.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 

      try{ 
       //do the work 
      } catch (Exception e){ 
       e.printStackTrace(); 
      } 

     } 
    }); 

    loginData.show(); 

    ..... 
+0

它工作完美! 非常感謝你! – user1256477 2012-03-20 11:06:09

0

我不能發佈你正確的代碼,但我認爲你必須使用fill_parent的寬度和高度。我總是用

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

在我的佈局xml文件中。

+0

FILL_PARENT實際上並不會做你在AlertDialog希望。請參閱我的答案,瞭解如何調整對話框的寬度和高度。 – Mimminito 2012-03-19 15:26:55

0

您將需要獲取顯示寬度,然後根據顯示寬度調整AlertDialog的LayoutParams。我建議你在寬度上添加10%的填充值,這樣整個屏幕就不會被填滿。

另一種選擇是將主題應用於類似於對話框的活動,這將允許您使用整個寬度。

相關問題