我在Android应用程序中遇到了自定义对话框的问题。
我有一个带有ListView的自定义对话框。 Dialog本身宽度为600dp,ListView也是600dp。
问题是对话框超过了宽度值:
这是对话框布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="600dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/promotionListView"
android:layout_width="600dp"
android:layout_height="400dp"
android:scrollbarStyle="insideOverlay"
android:dividerHeight="1dp" />
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/promotion_dialog_abort"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/annulla"/>
<Button
android:id="@+id/promotion_dialog_apply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/applica"/>
</LinearLayout>
</LinearLayout>
这是单行的布局:(注意布局的宽度和单个视图的总和都是600dp)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="600dp" <-- I tried "wrap_content" and "match_parent" with the same result
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/promotionCheck"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:layout_gravity="center" />
<TextView
android:id="@+id/promotionText"
android:layout_width="350dp"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_gravity="center"
android:textColor="@android:color/white"
android:textSize="@dimen/customerdetail_headertextsize" />
<TextView
android:id="@+id/promotionFactor"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center_vertical|right"
android:layout_gravity="center"
android:textColor="@android:color/white"
android:textSize="@dimen/customerdetail_headertextsize" />
<TextView
android:id="@+id/promotionPrice"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center_vertical|right"
android:layout_gravity="center"
android:textColor="@android:color/white"
android:textSize="@dimen/customerdetail_headertextsize" />
</LinearLayout>
最后是自定义对话类
public class PromotionDialog extends Dialog {
private boolean authCode;
private Set<OrderDetailDiscount> promotions;
private List<OrderDetailDiscount> selectedPromotions;
private ListView mainListView;
public Response response = Response.CANCEL;
private Button buttonAbort;
private Button buttonAccept;
public PromotionDialog(Context context, boolean authCode, Set<OrderDetailDiscount> promotions) {
super(context);
this.authCode = authCode;
this.promotions = promotions;
selectedPromotions = new ArrayList<>();
setTitle("Promozioni");
setContentView(R.layout.isfa_promotiondialog);
buttonAbort = (Button) findViewById(R.id.promotion_dialog_abort);
buttonAccept = (Button) findViewById(R.id.promotion_dialog_apply);
}
}
我真的无法理解问题是什么,我试图手动调整标题栏的大小但没有成功......我有可能尝试一下吗?
答案 0 :(得分:1)
您应使用wrap_content
或match_parent
代替固定的宽度/高度值,以支持不同的屏幕尺寸,并模拟可以layout_weight
应用于每个子(列)的表格行水平LinearLayout
的。
此外,我建议您使用AlertDialog.Builder
API。
以下是我尝试使用我的建议重新创建对话框的方法:
这是代码:
<强> MainActivity.java 强>
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
mBuilder.setTitle("Discounts");
mBuilder.setAdapter(new MyAdapter(), null);
mBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
mBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
mBuilder.create().show();
}
class RowItem{
public String field1;
public String field2;
public String field3;
public RowItem(String f1,String f2, String f3){
field1=f1;
field2=f2;
field3=f3;
}
}
private class MyAdapter extends BaseAdapter{
private final ArrayList<RowItem> rows = new ArrayList<RowItem>();
public MyAdapter(){
rows.add(new RowItem("Sales","1","5%"));
rows.add(new RowItem("Extra Sales","2","50%"));
rows.add(new RowItem("Super Sales","3","70%"));
rows.add(new RowItem("Completely Free","4","100%"));
}
@Override
public int getCount(){
return rows.size();
}
@Override
public Object getItem(int p){
return rows.get(p);
}
@Override
public long getItemId(int p){
return p;
}
@Override
public View getView(int position, View v, ViewGroup parent){
v=getLayoutInflater().inflate(R.layout.row_layout,parent,false);
LinearLayout row = (LinearLayout)v.findViewById(R.id.linearlayout);
CheckBox checkBox = (CheckBox)v.findViewById(R.id.checkbox);
TextView field1 = (TextView)v.findViewById(R.id.field1);
TextView field2 = (TextView)v.findViewById(R.id.field2);
TextView field3 = (TextView)v.findViewById(R.id.field3);
row.setBackgroundColor(Color.parseColor(position%2==0?"#212121":"#424242"));
RowItem item = rows.get(position);
field1.setText(item.field1);
field2.setText(item.field2);
field3.setText(item.field3);
return v;
}
}
}
<强> row_layout.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:orientation="horizontal"
android:weightSum="1"
android:layout_width="match_parent" android:layout_height="wrap_content">
<CheckBox
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/checkbox"
android:checked="false" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="myText"
android:id="@+id/field1"
android:layout_weight="0.6" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="myText"
android:id="@+id/field2"
android:layout_weight="0.15" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="myText"
android:id="@+id/field3"
android:layout_weight="0.15" />
</LinearLayout>