DialogFragment中的RelativeLayout - 忽略MarginBottom

时间:2017-04-10 07:36:49

标签: android margin relativelayout

所以基本上,我有一个全屏的DialogFragment,并且在进度条上有一个按钮(如果单击按钮,它会消失,进度条可见)。我最初在一个活动中使用了xml,一切都按预期工作,但现在我想将它重构为全屏对话碎片。

问题是,进度条上方的按钮现在与parentLayout的底部对齐

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_gps_loading"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_setsensor"
    tools:ignore="MissingPrefix"
    >

  <ImageView
      android:layout_width="90dp"
      android:layout_height="38dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="45dp"
      />

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_marginLeft="40dp"
      android:layout_marginRight="40dp"
      android:orientation="vertical"
      >

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_location"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="17dp"
        android:gravity="center_horizontal"
        android:text="@string/promptGpsPermissionBody"
        style="@style/fullscreenText"
        />
  </LinearLayout>

  <ProgressBar
      android:id="@+id/progressBar2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/btnActivateGps"
      android:layout_alignTop="@+id/btnActivateGps"
      style="?android:attr/progressBarStyle"
      />

  <Button
      android:id="@+id/btnActivateGps"
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="71dp"
      android:layout_marginLeft="63dp"
      android:layout_marginRight="63dp"
      android:background="@color/white"
      android:enabled="true"
      android:visibility="visible"
      />
</RelativeLayout>

特别奇怪的是,进度条与按钮对齐,但它仍然位于与之前相同的位置

这是java代码

public class GpsSensorDialog extends DialogFragment {

  @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    View view = inflater.inflate(R.layout.activity_gps_sensor, null);

    builder.setView(view);

    Dialog dialog = builder.create();

    if (dialog.getWindow() != null) {
      dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
      dialog.getWindow()
          .setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }
    return dialog;
  }

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE, R.style.CustomDialog);
  }

  public static GpsSensorDialog newInstance() {
    Bundle args = new Bundle();
    GpsSensorDialog fragment = new GpsSensorDialog();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onStart() {
    super.onStart();
    getDialog().getWindow()
        .setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  }
}

和我在onCreate中设置的样式

<style name="CustomDialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

1 个答案:

答案 0 :(得分:0)

试试这个布局。此外,相应地更改您的drawable,样式和字符串。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_gps_loading"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/ic_launcher"
    tools:ignore="MissingPrefix">

    <ImageView
        android:layout_width="90dp"
        android:layout_height="38dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:orientation="vertical">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="17dp"
            android:gravity="center_horizontal"
            android:text="obsdas" />
    </LinearLayout>

    <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnActivateGps" />

    <Button
        android:id="@+id/btnActivateGps"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"  
        android:layout_marginBottom="71dp"
        android:layout_marginLeft="63dp"
        android:layout_marginRight="63dp"
        android:background="@android:color/white"
        android:enabled="true"
        android:visibility="visible" />
</RelativeLayout>