【问题标题】:Setting margins on RelativeLayout doesnt work on Kindle Fire在 RelativeLayout 上设置边距在 Kindle Fire 上不起作用
【发布时间】:2012-04-06 12:27:47
【问题描述】:

这类似于我的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 是 FrameLayout 还是 RelativeLayout 的子视图?
  • infoLayout 是一个相对布局
  • 你为什么使用 FrameLayout.LayoutParams 而不是 RelativeLayout.LayoutParams?您是否尝试过使用 RelativeLayout.LayoutParams?更重要的是,您是否尝试过调用 params = infoLayout.getParams();然后编辑参数而不是重新创建它们?
  • 我使用 FrameLayout 是因为我将片段添加到其中,这确实适用于 3.2.1 设备,但不适用于 Kindle。我也尝试过使用 LinearLayout 和 RelativeLayout,但这些都不起作用。我也不能在 infolayout 上调用 getparam(),因为没有这样的方法。
  • 好吧,我是误会了。我看不出你的所作所为有什么明显的错误。您可以做一些非常骇人听闻的事情,并在片段的上方和左侧添加通用的不可见视图,并设置它们的宽度,这将为您提供与设置边距相同的效果。您可能还想检查您的其他代码,以防您有其他间接导致问题的错误。 kindle 可能对错误代码做出正确反应并产生错误的输出,而另一台设备可能会做出错误的反应以产生正确的输出。

标签: android layout margin android-relativelayout kindle-fire


【解决方案1】:

好的,我可以通过将我的 infoLayout 包装在另一个 RelativeLayout 中来解决这个问题,现在它可以完美地工作了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多