我正在创建一个alertDialog来请求登录数据。
我想使用所有水平空间,但我无法得到它:
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 :(得分: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文件中。
答案 1 :(得分:0)
您需要获取显示的宽度,然后根据显示宽度调整AlertDialog的LayoutParams。我建议你在宽度上添加10%的填充,这样整个屏幕就不会被填充。
另一个选择是将一个主题应用于类似对话框的活动,这将允许您使用整个宽度。
答案 2 :(得分: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();
.....