这与我的previous question类似,但不适用于Kindle Fire(2.3.4)。
我使用FragmentTransaction将片段添加到FrameLayout中。我想动态更改Fragment使用的RelativeLayout的边距。

但是,Kindle Fire上的FrameLayout.layoutParams不会改变边距。但是,这适用于3.2.1。我也尝试过使用setMargins(),但是没有用。

有谁知道我该如何动态改变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;
    }

最佳答案

好的,我可以通过将infoLayout封装在另一个RelativeLayout中来解决此问题,现在它可以完美运行

10-04 22:40
查看更多