对话框布局在2.3.3上更改

时间:2014-10-01 18:35:52

标签: android android-layout

我正在尝试为我的应用创建自定义对话框,我希望它在旧API中也类似。以下是我对Jelly Bean(API 17)的看法:

http://s3.postimg.org/xthkyt1jn/image.png

看起来很棒:)确切地说我想要的方式)

然后我在GingerBread(API 10)上测试它,这就是

http://s23.postimg.org/w2fc4fccb/oldshit.png

请帮帮我。我不知道为什么我的按钮缩小了。

这是我的对话框的基本布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentPanel"
    android:background="@drawable/roundcorner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dip"
    android:layout_marginEnd="8dip"
    android:orientation="vertical">

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

        <LinearLayout android:id="@+id/title_template"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical|start"
            android:minHeight="64dip"
            android:layout_marginStart="16dip"
            android:layout_marginEnd="16dip">
            <ImageView android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingEnd="8dip"
                android:layout_marginLeft="8dip"
                android:src="@null" 
                android:contentDescription="@string/alert_image_content_description"/>
            <com.android.internal.widget.DialogTitle 
                android:id="@+id/alertTitle"
                android:singleLine="true"
                android:ellipsize="end"
                style="@style/QustomDialogTitleForOlderPlatfoms"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dip" />
        </LinearLayout>
        <View android:id="@+id/titleDivider"
            android:layout_width="match_parent"
            android:layout_height="2dip"
            android:visibility="visible"
            android:background="#FFFFFF" />
    </LinearLayout>

    <FrameLayout android:id="@+id/customPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/roundcorner">
    </FrameLayout>

</LinearLayout>

然后在java代码中我为FrameLayout调用addView()并添加使用此布局充气的View

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/roundcorner"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/product_name"
            style="@style/EditTextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="6dp"
            android:layout_marginTop="6dp"
            android:ems="10"
            android:hint="Enter your product title" >

            <requestFocus />
        </EditText>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="6dp"
            android:layout_marginTop="6dp"
            android:orientation="horizontal" >

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" >

                <EditText
                    android:id="@+id/product_amount"
                    style="@style/EditTextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="Enter your product amount" />
            </FrameLayout>

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="6dp"
                android:layout_weight="1" >

                <Spinner
                    android:id="@+id/measures_spinner"
                    style="@style/EditTextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </FrameLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/cancel_product_creation"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/custom_button"
                android:padding="4dp"
                android:text="Cancel"
                android:textColor="#FFFFFF" />

            <Button
                android:id="@+id/add_new_product_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/custom_button"
                android:padding="4dp"
                android:text="Add"
                android:textColor="#FFFFFF" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

这是我的Dialog代码

public class CustomDialog extends Dialog{

/** The custom_body layout */
private View mDialogView;

/** optional dialog title layout */
private TextView mTitle;
/** optional alert dialog image */
private ImageView mIcon;
/** The colored holo divider. You can set its color with the setDividerColor method */
private View mDivider;

private View customView;


public CustomDialog(Context context) {

    super(context);

    mDialogView = View.inflate(context, R.layout.qustom_dialog_layout, null);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(mDialogView);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));      
    mTitle = (TextView) mDialogView.findViewById(R.id.alertTitle);
    mIcon = (ImageView) mDialogView.findViewById(R.id.icon);
    mDivider = mDialogView.findViewById(R.id.titleDivider);
}

/** 
 * Use this method to color the divider between the title and content.
 * Will not display if no title is set.
 * 
 * @param colorString for passing "#ffffff"
 */
public CustomDialog setDividerColor(String colorString) {
    mDivider.setBackgroundColor(Color.parseColor(colorString));
    return this;
}

public void setTitle(CharSequence text) {
    mTitle.setText(text);
}

public void setTitleColor(String colorString) {
    mTitle.setTextColor(Color.parseColor(colorString));
}


public void setIcon(int drawableResId) {
    mIcon.setImageResource(drawableResId);
}

public void setIcon(Drawable icon) {
    mIcon.setImageDrawable(icon);
}

/**
 * This allows you to specify a custom layout for the area below the title divider bar
 * in the dialog. As an example you can look at example_ip_address_layout.xml and how
 * I added it in TestDialogActivity.java
 * 
 * @param resId  of the layout you would like to add
 * @param context
 */
public CustomDialog setCustomView(int resId, Context context) {
    customView = View.inflate(context, resId, null);
    ((FrameLayout)mDialogView.findViewById(R.id.customPanel)).addView(customView);
    return this;
}

public View getCustomView()
{
    return customView;
}

@Override
public void show() {
    if (mTitle.getText().equals("")) mDialogView.findViewById(R.id.topPanel).setVisibility(View.GONE);
    super.show();
}

}

这是我如何使用它

dialog = new CustomDialog(getActivity());
     dialog.setCustomView(R.layout.add_new_item, getActivity());
     dialog.setTitle("Add New Product");
     dialog.setTitleColor("#FFFFFF");
     dialog.setDividerColor("#FFFFFF");
     dialog.setIcon(R.drawable.ic_action_new);

UPD1 /// 我发现问题是FrameLayout@id/customPanel)没有为layout_height = "wrap_content"提供足够的空间来实际包装它的内容。如果我在dp中设置它的布局高度 - 一切都很好,但它在更大的屏幕上留下了大量的自由空间。那么我应该如何强制这个FrameLayout显示它的所有内容呢?

1 个答案:

答案 0 :(得分:0)

更改此代码:

public CustomDialog setCustomView(int resId, Context context) {
    customView = View.inflate(context, resId, null);
    ((FrameLayout)mDialogView.findViewById(R.id.customPanel)).addView(customView);
    return this;
}

要:

public CustomDialog setCustomView(int resId, Context context) {
    FrameLayout root = (FrameLayout)mDialogView.findViewById(R.id.customPanel));
    LayoutInflater inflater = LayoutInflater.from(context);
    customView = inflater.inflate(resId, root, false);
    root.addView(customView);
    return this;
}

如果有帮助,请告诉我。

//编辑:

另外,看看http://developer.android.com/reference/android/app/AlertDialog.html#setView(android.view.View) - 也许这比你当前的CustomDialog实现更好。