Вид на дочерний макет отображается под видом на родительский макет - PullRequest
1 голос
/ 01 ноября 2019

Я собираюсь создать пульт дистанционного управления с динамичным внешним видом. Итак, я хочу добавить все кнопки программно. У каждой кнопки есть фон, значок и ?selectableItemBackgroundBorderless рисунки. Согласно этому руководству я использовал два макета, чтобы получить фон без полей над фоном кнопки. Проблема заключается в том, что представление, добавляемое в родительский макет, отображается над представлением, которое добавляется в дочерний макет, и закрывает его.

фрагмент_remote_control.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/ir_bg">

    <FrameLayout
        android:id="@+id/frag_remote_button_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent" />

</FrameLayout>

Фрагмент onCreateView():

public View onCreateView(@NonNull LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.fragment_remote_control,
            container, false);
    FrameLayout buttonLayout = layout.findViewById(R.id.frag_remote_button_container);

    mRemoteControl = new ACRemoteControl(mDevice, mIRControl);
    mRemoteControl.drawInterface(inflater.getContext(), layout, buttonLayout,
            ScreenInfo.getByResources(getResources()));

    return layout;
}

RemoteControl.java:

public abstract class RemoteControl {

    private static int getSelectableItemBackgroundResID(@NonNull Context context) {

        TypedValue outValue = new TypedValue();
        if (Build.VERSION.SDK_INT >= 21) {
            context.getTheme()
                    .resolveAttribute(R.attr.selectableItemBackgroundBorderless, outValue, true);
        } else {
            context.getTheme()
                    .resolveAttribute(R.attr.selectableItemBackground, outValue, true);
        }
        return outValue.resourceId;
    }

    protected ImageButton placeCircleButton(@NonNull Context context,
                                            @NonNull FrameLayout layout,
                                            @NonNull FrameLayout buttonLayout,
                                            int x, int y, int iconResID) {

        Resources res = context.getResources();
        int sizeFull = res.getDimensionPixelSize(R.dimen.ir_bg_circle_full_size);
        int size = res.getDimensionPixelSize(R.dimen.ir_bg_circle_size);
        FrameLayout.LayoutParams lp;

        View bg = new View(context);
        lp = new FrameLayout.LayoutParams(sizeFull, sizeFull);
        lp.leftMargin = x - sizeFull / 2;
        lp.topMargin = y - sizeFull / 2;
        layout.addView(bg, lp);
        bg.setBackgroundResource(R.drawable.ir_button_circle_bg);

        ImageButton imageButton = new ImageButton(context);
        lp = new FrameLayout.LayoutParams(size, size);
        lp.leftMargin = x - size / 2;
        lp.topMargin = y - size / 2;
        buttonLayout.addView(imageButton, lp);
        imageButton.setBackgroundResource(getSelectableItemBackgroundResID(context));
        imageButton.setImageResource(iconResID);

        return imageButton;
    }

    public abstract void drawInterface(@NonNull Context context,
                                       @NonNull FrameLayout layout,
                                       @NonNull FrameLayout buttonLayout,
                                       @NonNull ScreenInfo screenInfo);

    protected RemoteControl(byte type) {
        this.type = type;
    }
}

ACRemoteControl.java

public class ACRemoteControl extends RemoteControl {

    private final IRControl mIRControl;

    @Override
    public void drawInterface(@NonNull Context context,
                              @NonNull FrameLayout layout,
                              @NonNull FrameLayout buttonLayout,
                              @NonNull ScreenInfo screenInfo) {

        placeCircleButton(context, layout, buttonLayout,
                screenInfo.getWidth() / 2, screenInfo.getHeight() / 2,
                R.drawable.round_power_settings_new_black_24);
    }

    public ACRemoteControl(@NonNull GeneralDevice device,
                           @NonNull IRControl irControl) {
        super(TYPE_AIR_CONDITIONER);
        mIRControl = irControl;
    }
}

Я ожидаю получить это:

Expected result

Но на практике я получаю это:

Obtained result

...