Android:自定义对话框的UI问题

时间:2014-04-01 07:11:17

标签: android dialog

我需要在我的应用中显示对话框,如下所示:

Desired output

早些时候我使用的是Popup window,因此它显示正确。但是,我稍后将其更改为DialogFragment,因为与此类似的其他对话框将具有ListViews and Tabs。因此,使用DialogFragment会更灵活。

将其更改为DialogFragment后,显示如下: Present output

这是我的Dialog Fragment代码:

public class AddTransactionDialogFragment extends DialogFragment {

private final String TAG = "AddTransactionDialog";
private View anchorView; // Anchor view here in Dollar button

private int dialogHeight, dialogWidth; // Width and height are the size this
                                        // dialog should have

private View contentView;

public AddTransactionDialogFragment() {
}

public AddTransactionDialogFragment(View anchorView, int dialogHeight,
        int dialogWidth) {
    this.anchorView = anchorView;
    this.dialogHeight = dialogHeight;
    this.dialogWidth = dialogWidth;
    Log.e(TAG, "Dialog width: " + dialogWidth + " Dialog height: "
            + dialogHeight);
}

public static AddTransactionDialogFragment newInstance(
        FriendsDAO mFriendsDao) {
    AddTransactionDialogFragment fragment = new AddTransactionDialogFragment();

    Bundle args = new Bundle();
    args.putParcelable("friendsDao", mFriendsDao);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setStyle(STYLE_NO_FRAME, R.style.AddTransactionDialog);
}

@Override
public void onStart() {
    super.onStart();

    Window window = getDialog().getWindow();
    WindowManager.LayoutParams params = window.getAttributes();
    params.dimAmount = 0.2f;
    window.setAttributes(params);

    window.setBackgroundDrawableResource(android.R.color.transparent);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    contentView = inflater.inflate(R.layout.add_transaction_popup,
            container, false);
    setDialogPosition();
    return contentView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

private void setDialogPosition() {
    if (anchorView == null) {
        return;
    }
    int[] location = new int[2];
    anchorView.getLocationOnScreen(location);
    int anchorViewX = location[0];
    int anchorViewY = location[1];

    Log.e(TAG, "AnchorViewX: " + anchorViewX + " AnchorViewY: "
            + anchorViewY);
    Window window = getDialog().getWindow();

    window.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

    window.setBackgroundDrawable(new ColorDrawable(
            android.graphics.Color.TRANSPARENT));

    WindowManager.LayoutParams params = window.getAttributes();
    params.copyFrom(window.getAttributes());

    Log.e("AddTransactionDialog",
            "Anchor view width: " + anchorView.getWidth());
    Log.e("AddTransactionDialog",
            "Anchor view height: " + anchorView.getHeight());

    params.x = anchorViewX - anchorView.getWidth();
    params.y = anchorViewY - anchorView.getHeight();
    params.height = dialogHeight;
    params.width = dialogWidth;
    window.setAttributes(params);
}

public int dpToPx(float valueInDp) {
    DisplayMetrics metrics = getActivity().getResources()
            .getDisplayMetrics();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            valueInDp, metrics);
}
}

add_transaction_popup.xml

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

<include
    android:id="@+id/header"
    android:layout_width="wrap_content"
    layout="@layout/transactionheader" />

<RelativeLayout
    android:id="@+id/relTransactionPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/header"
    android:layout_alignRight="@+id/header"
    android:layout_below="@+id/header"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp" >

    <LinearLayout
        android:id="@+id/linButtonPanel"
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnIOwe"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="0.5"
            android:background="@drawable/btn_pink"
            android:text="@string/i_owe"
            android:textColor="@color/red_shade_2"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btnTheyOwe"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="0.5"
            android:background="@drawable/btn_green"
            android:text="@string/they_owe"
            android:textColor="@color/green_shade_2"
            android:textSize="18sp" />
    </LinearLayout>

    <EditText
        android:id="@+id/edtTransactionDesc"
        style="@style/transactioninputTextStyle"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/linButtonPanel"
        android:layout_alignRight="@+id/linButtonPanel"
        android:layout_below="@+id/linButtonPanel"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:background="@drawable/transaction_edittext_bg"
        android:hint="@string/description"
        android:paddingLeft="10dp" />

    <EditText
        android:id="@+id/edtTransactionAmt"
        style="@style/transactioninputTextStyle"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/linButtonPanel"
        android:layout_alignRight="@+id/linButtonPanel"
        android:layout_below="@+id/edtTransactionDesc"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:background="@drawable/transaction_edittext_bg"
        android:hint="@string/amount"
        android:inputType="number"
        android:paddingLeft="10dp" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/relAddTransaction"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/relTransactionPanel"
    android:layout_marginTop="100dp" >

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="18dp"
        android:background="@color/green_shade_3" />

    <TextView
        android:id="@+id/txtAddTransaction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/add"
        android:textColor="@color/green_shade_3"
        android:textSize="18sp" />

    <ImageView
        android:id="@+id/imgPay"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/view1"
        android:layout_marginRight="18dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/pay_green" />

    <View
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_below="@+id/imgPay"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="18dp"
        android:background="@color/green_shade_3" />
</RelativeLayout>

transactionheader.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="18dp"
android:layout_marginTop="15dp"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/friendsImage"
    android:orientation="vertical" >

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="2dp"
        android:background="@color/bro_name_color" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="6dp"
        android:background="@color/bro_name_color" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="2dp"
        android:layout_marginTop="6dp"
        android:background="@color/bro_name_color" />
</LinearLayout>

<com.dzo.moneybro.ui.CircularImageView
    android:id="@+id/friendsImage"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:layout_centerInParent="true"
    android:src="@drawable/rotatetrans" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/friendsImage"
    android:orientation="vertical" >

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="5dp"
        android:background="@color/bro_name_color" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="6dp"
        android:background="@color/bro_name_color" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="25dp"
        android:layout_marginTop="6dp"
        android:background="@color/bro_name_color" />
</LinearLayout>

如何使用对话框片段获得所需的对话框(如第一张图片所示)?

1 个答案:

答案 0 :(得分:0)

对于内容视图,您正在设置DialogFragment,您是否尝试将layout_height设置为wrap_content?

这里的问题是layout_height是match_parent。