带边距的半透明对话框

时间:2016-05-28 16:07:31

标签: android

我需要创建半透明对话框,从顶部开始有100dp的边距 在我的代码中,我添加了这个:

 Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Translucent_NoTitleBar);
  dialog.setContentView(R.layout.transparent_progress_bar);

这是transparent_progress_bar

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_marginTop="100dp"
        >

<ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:indeterminate="true"
        android:indeterminateDrawable="@drawable/rotate_progress_bar"
        android:indeterminateOnly="true"
        android:padding="@dimen/android_spacing_double"/>

</RelativeLayout>

但它不起作用。
关于如何修改主题或为此添加边距的任何想法?

2 个答案:

答案 0 :(得分:0)

margin会将您的布局与父布局对齐

使用android:paddingTop代替android:layout_marginTop

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:paddingTop="100dp"
        >
        ...

</RelativeLayout>

更新

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <RelativeLayout
             android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:layout_marginTop="100dp">
            ...

        </RelativeLayout>

</RelativeLayout> 

答案 1 :(得分:0)

试试这个!!

透明_progress_bar.xml

中的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_dialog_control"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@android:color/transparent"
 android:layout_marginTop="8dp"
>

<View
    android:id="@+id/view_close_top"
    android:layout_width="match_parent"
    android:layout_height="100dp" />

<RelativeLayout
    android:id="@+id/relative_popup"
    android:layout_width="match_parent"
    android:layout_height="207dp"
    android:layout_below="@+id/view_close_top"
    android:layout_marginLeft="9dp"
    android:layout_marginRight="9dp"
    android:background="#FFFFFF"
    android:padding="0dp">

   <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center"
    android:background="@android:color/transparent"
    android:indeterminate="true"
    android:indeterminateDrawable="@drawable/rotate_progress_bar"
    android:indeterminateOnly="true"
    android:padding="@dimen/android_spacing_double"/>
</RelativeLayout>

<View
    android:id="@+id/view_close"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/relative_popup" />
</RelativeLayout>
res / value / style.xml中的

添加

 <style name="Theme.Transparent" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="windowNoTitle">true</item>
</style>

这是show dialog

时的代码
 private void showPopupControl() {
    final AppCompatDialog dialog = new AppCompatDialog(MainActivity.this, R.style.Theme_Transparent);
      //dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    dialog.setContentView(R.layout.transparent_progress_bar);
    ViewGroup.LayoutParams params = dialog.getWindow().getAttributes();
    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
    dialog.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

    dialog.findViewById(R.id.relative_dialog_control).setVisibility(View.VISIBLE);
    dialog.findViewById(R.id.view_close).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
    dialog.findViewById(R.id.view_close_top).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });

    dialog.show();
}

<强> happyCoding;