android - 在 RelativeLayout 上设置边距在 Kindle Fire 上不起作用

标签 android layout margin android-relativelayout kindle-fire

这类似于我的 previous question但它不适用于 Kindle Fire (2.3.4)。
我使用 FragmentTransaction 将 Fragment 添加到 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 中来解决这个问题,现在它可以完美地工作了

关于android - 在 RelativeLayout 上设置边距在 Kindle Fire 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9813894/

相关文章:

android - Activity 在屏幕旋转时重新启动我的游戏 (Android)

android - 如何修复无法使用 Jetifier 转换 kotlin-android-extensions-1.3.71.jar' 错误?

android - 如何将元素动态添加到使用 XML 创建的 View 中

android - SwipeRefresh 布局不适用于自定义空 ListView

单击图标时 Android DrawerLayout 崩溃

templates - 为什么我的 Magento 产品页面的评论部分没有显示?

HTML 布局问题面板全高

html - 表格 : Trying to align label and text area elements? 的 margin 基础知识

Safari 中的 CSS 全宽背景图像(负边距)

css - IE 问题 - 图像周围不需要的边距