Android:自定义对话计算器在Android 2.2.1和2.3.3上有所不同

时间:2011-07-31 23:57:02

标签: android xml layout dialog android-2.3-gingerbread

我创建了一个自定义对话框,它是一个简单的计算器。我在Android 2.3.3中遇到了布局问题。该对话框将计算器完美地包含在2.2.1及更低版本中,但在2.3.3中却没有。下面我发布了正在发生的图片和布局的XML代码。下面是关于我如何启动Dialog的代码。感谢您的帮助!!!

dialog on HTC thunderbold 2.2.1 http://img20.imageshack.us/img20/6981/htc221.png

dialog on droidX 2.3.3 http://img64.imageshack.us/img64/876/droidx233.png

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:gravity="center"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >
  <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <EditText 
        android:id="@+id/calc_dialog_display"
        android:textSize="@dimen/calc_dialog_text_size" 
        android:editable="false"
        android:clickable="false"
        android:focusable="false"
        android:cursorVisible="false"
        android:gravity="right"     
        android:layout_alignParentTop="true" 
        android:layout_centerHorizontal="true"
        android:layout_width="238dp"
        android:layout_height="wrap_content" />
    <EditText 
        android:id="@+id/calc_dialog_display_fake"
        android:textSize="@dimen/calc_dialog_text_size" 
        android:visibility="invisible"
        android:editable="false"
        android:clickable="false"
        android:focusable="false"
        android:cursorVisible="false"
        android:gravity="right"     
        android:layout_alignParentTop="true" 
        android:layout_centerHorizontal="true"
        android:layout_width="238dp"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/enter_total"
        android:text="@string/calc_dialog_enter_total_button"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/calc_dialog_display"
        android:layout_alignParentLeft="true"
        android:layout_width="180dp"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/all_clear"
        android:text="AC"
        android:textSize="@dimen/calc_dialog_text_size"
        android:textColor="@color/all_clear_color"
        android:layout_below="@id/calc_dialog_display"
        android:layout_toRightOf="@id/enter_total"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/seven"
        android:text="7"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/enter_total"
        android:layout_alignParentLeft="true"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/eight"
        android:text="8"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/enter_total"
        android:layout_toRightOf="@id/seven"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/nine"
        android:text="9"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/enter_total"
        android:layout_toRightOf="@id/eight"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/division"
        android:text="÷"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/enter_total"
        android:layout_toRightOf="@id/nine"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/four"
        android:text="4"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/seven"
        android:layout_alignParentLeft="true"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/five"
        android:text="5"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/seven"
        android:layout_toRightOf="@id/four"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/six"
        android:text="6"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/seven"
        android:layout_toRightOf="@id/five"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/multiply"
        android:text="x"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/seven"
        android:layout_toRightOf="@id/six"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/one"
        android:text="1"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/four"
        android:layout_alignParentLeft="true"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/two"
        android:text="2"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/four"
        android:layout_toRightOf="@id/one"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/three"
        android:text="3"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/four"
        android:layout_toRightOf="@id/two"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/subtract"
        android:text="-"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/four"
        android:layout_toRightOf="@id/three"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/decimal"
        android:text="."
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/one"
        android:layout_alignParentLeft="true"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/zero"
        android:text="0"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/one"
        android:layout_toRightOf="@id/decimal"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/equals"
        android:text="="
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/one"
        android:layout_toRightOf="@id/zero"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />
    <Button
        android:id="@+id/addition"
        android:text="+"
        android:textSize="@dimen/calc_dialog_text_size"
        android:layout_below="@id/one"
        android:layout_toRightOf="@id/equals"
        android:layout_width="@dimen/calc_dialog_button_width"
        android:layout_height="@dimen/calc_dialog_button_height" />     
    </RelativeLayout>
</RelativeLayout>

@Override
protected Dialog onCreateDialog(int id) {
    super.onCreateDialog(id);
    Dialog dialog = null;

    switch(id){
    case 1:
        dialog = new CustomCalcDialog(this, shippingPercent, QUICK_CALC_TRIGGER);
        dialog.setTitle("Enter Shipping %");
        break;
    case 2:
        dialog = new CustomCalcDialog(this, taxPercent, QUICK_CALC_TRIGGER);
        dialog.setTitle("Enter Tax Rate");
        break;
    case 3:
        dialog = new CustomCalcDialog(this, commissionPercent, QUICK_CALC_TRIGGER);
        dialog.setTitle("Enter Commission %");
        break;
    case 4:
        dialog = new CustomCalcDialog(this, productSubtotal, QUICK_CALC_TRIGGER);
        dialog.setTitle("Calculate Subtotal");
        break;
    case 5:
        dialog = new CustomCalcDialog(this, addShipping, QUICK_CALC_TRIGGER);
        dialog.setTitle("Additional Shipping");
        break;
    case 6:
        dialog = new BackgroundOptionsDialog(this, quickCalcBackground);
        dialog.setTitle("Choose Background:");
        break;
    case 7:
        dialog = new CustomCalcDialog(this, shippingDollar, QUICK_CALC_TRIGGER);
        dialog.setTitle("Enter Shipping $");
        break;
    }
    return dialog;
}

2 个答案:

答案 0 :(得分:0)

尝试将relativelayout的宽度设置为fill_parent

答案 1 :(得分:0)

行。我发现了一个问题。在自定义对话框的onCreate()中插入以下代码。

Window windowProperties = getWindow();
windowProperties.setBackgroundDrawable(null);

将setBackgroundDrawable()设置为null会使背景变为不透明。这解决了这个问题,它仍适用于较低版本的Android。

The Fixed Picture http://img827.imageshack.us/img827/5428/droidxfix233.png

感谢所有建议