Setting margins on RelativeLayout doesnt work on K

2019-06-07 20:40发布

问题:

This is similar to my previous question but it didnt work with the Kindle Fire (2.3.4).
I used a FragmentTransaction to add a Fragment into a FrameLayout. I want to dynamically change the margin of the RelativeLayout used by the Fragment.

However, the margins are not changing with FrameLayout.layoutParams on the Kindle Fire. However, this works on 3.2.1. I also tried using setMargins() and it didnt work.

Does anyone know how I can dynamically change the margins on the Kindle Fire?

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

        View view = inflater.inflate(R.layout.infocard, container, false);

        RelativeLayout infoLayout = (RelativeLayout) view.findViewById(R.id.info);
        infoLayout.setOnClickListener(new EmptyClickListener());

        final int width = 250;
        final int height = 270;
        int leftMargin = 0;
        int topMargin = 0;
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);

        if (x - width < 0) {
            leftMargin = 0;
        } else {
            leftMargin = x - width + 40;
        }

        if (y >= 400 && y <= 480) {
            topMargin = 160;
        }

        params.leftMargin = leftMargin;
        params.topMargin = topMargin;

        //params.setMargins(leftMargin, topMargin, 0, 0); //this didnt work either
        infoLayout.setLayoutParams(params);

        TextView titleView = (TextView) view.findViewById(R.id.titleCard);
        titleView.setText(title);

        return view;
    }

回答1:

Ok I was able to fix this by wrapping my infoLayout in another RelativeLayout, and now it works perfectly