相对布局不工作设置页边距 [英] setting margins on relative layout doesnt work

查看:266
本文介绍了相对布局不工作设置页边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用FragmentTransaction一个片段加入到一个的FrameLayout。我想动态改变的RelativeLayout由片段中的余量。然而,利润率不与RelativeLayout.layoutParams改变。我还用 setMargins()并没有工作。

 公开查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){        查看查看= inflater.inflate(R.layout.infocard,集装箱,FALSE);        RelativeLayout的infoLayout =(RelativeLayout的)view.findViewById(R.id.info);
        infoLayout.setOnClickListener(新EmptyClickListener());        最终诠释宽度= 250;
        最终诠释高= 320;
        INT LEFTMARGIN = 0;
        INT TOPMARGIN = 0;
        RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(宽度,高度);        如果(X - 宽度小于0){
            LEFTMARGIN = 0;
            的System.out.println(左边的+ LEFTMARGIN);
        }
        其他{
            LEFTMARGIN = X - 宽度;
        }        如果(Y> = 450安培;&安培; Y< = 480){
            TOPMARGIN = Y - 高度;
        }        params.leftMargin = LEFTMARGIN;
        params.topMargin = TOPMARGIN;        infoLayout.setLayoutParams(PARAMS);


解决方案

尝试使用

  FrameLayout.LayoutParams

而不是

  RelativeLayout.LayoutParams

布局预期PARAMS类型为从父容器的类型,而不是要设置他们在视图类型

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 RelativeLayout.layoutParams. I also used setMargins() and it didnt work.

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 = 320;
        int leftMargin = 0;
        int topMargin = 0;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);

        if (x - width < 0) {
            leftMargin = 0;
            System.out.println("left " + leftMargin);
        }
        else {
            leftMargin = x - width;
        }

        if (y >= 450 && y <= 480) {
            topMargin = y - height;
        }

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

        infoLayout.setLayoutParams(params);

解决方案

Try using

FrameLayout.LayoutParams

instead of

RelativeLayout.LayoutParams

Layouts expect the params type to be the type from their parent container rather than the View type that you are setting them on

这篇关于相对布局不工作设置页边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆