有些 RelativeLayout.LayoutParams 不起作用

Some RelativeLayout.LayoutParams doesn't work

背景:我正在制作一个应用程序,它可以创建基于时间的警报、基于位置的警报或两者兼而有之,并且在 activity 中创建警报我有每个时间选择器和地图的复选框,如果您取消选中它您不想在闹钟中使用这些功能之一。这是 activity-http://prntscr.com/dr1s74 的图片。

如果您取消选中地图复选框,它会删除片段并将 radioGroup 的布局参数设置到标题下方(因为地图现在已经消失),将其居中并添加边距。

虽然 addRule(RelativeLayout.BELOW, id) 有效,但 setMargins(left, top, right, bottom) 和 addRule(RelativeLayout.CENTER_HORIZONTAL) 均无效,因此 radioGroup 已关闭居中,标题下方没有正确的 space。

XML Activity:

可能问题的描述:我认为它发生的唯一原因是我有 CoordinatorLayout 作为 parent 布局(因为它在 FAB 和 SnackBar 之间的交互)和它里面的 ScrollView 包含 RelativeLayout(打开到如果你有更好的方法,建议)。并且出于某种原因,在尝试向布局参数添加规则时会出现问题。 XML 代码(删除了单选按钮和复选框以便于查看):

<android.support.design.widget.FloatingActionButton
    android:id="@+id/saveAlarm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="@dimen/fab_margin"
    android:layout_marginRight="@dimen/fab_margin"
    android:onClick="fabClicked"
    app:srcCompat="@android:drawable/ic_menu_save" />

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/relative_layout_alarm_creator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.rome.locationalarm.AlarmCreator">

        <EditText
            android:id="@+id/alarmTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:hint="Alarm title" />

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_below="@+id/alarmTitle"
            android:layout_marginTop="10dp"
            tools:context="com.example.rome.locationalarm.MapsFragment" />

        <com.example.rome.locationalarm.CustomTimePicker
            android:id="@+id/timePicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/days"
            android:layout_marginTop="10dp" />


        <TextView
            android:id="@+id/chooseLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/map"
            android:layout_centerHorizontal="true"
            android:text="Choose Location" />


        <RadioGroup
            android:id="@+id/days"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/map"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

        </RadioGroup>

    </RelativeLayout>
</ScrollView>

JAVA 代码(仅重要部分):

 if(locationCheckBox.isChecked()){
                //If user wants location
                RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.addRule(RelativeLayout.BELOW, mapFragment.getId());
                //All of these don't work 
                layoutParams.setMargins(0, 10, 0, 0); // (left, top, right, bottom)
                //All of these are tries to center the radioGroup
                layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
                layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
                layout.removeView(radioGroup);
                layout.addView(radioGroup, layoutParams);
                //layout.updateViewLayout(radioGroup, layoutParams); doesn't work as well.

感谢您花时间提供帮助,祝您新年快乐!

您需要执行此操作才能获取 LayoutParams

 RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();