zoukankan      html  css  js  c++  java
  • RelativeLayout.LayoutParams 使用addRule失效的问题解决办法

    Button btn1;

    btn1.setId(1001);

    int width;   // layout width;

    int btnWidth; // btn width;

    int btnHeight; // btn height;

    …….

    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(btnWidth, btnHeight);

            p.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    p.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

    p.topMargin = config.topMargin;

    // 这里必须设置,否则依赖这个左右对齐的控件无法对齐。

    //   如果不设置,只使用水平居中的属性,则1001这个控件的leftmargin为0. 导致左右对齐失败

    p.leftMargin = (width-btnWidth)/2; 

    //

    Button btn2;

    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(btnWidth, btnHeight);

            p.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    p.addRule(RelativeLayout.ALIGN_LEFT, 1001); // 这里要想生效必须,1001这个控件必须添加leftmargin.

     p.topMargin = config.topMargin;

    //

    Button btn3;

    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(btnWidth, btnHeight);

            p.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    // 这里要想生效必须,1001这个控件必须添加leftmargin/ right margin.

    p.addRule(RelativeLayout.ALIGN_RIGHT, 1001); 

     p.topMargin = config.topMargin;

  • 相关阅读:
    ubuntu 1510
    创业者的困境
    创业者的窘境
    (转)Ubuntu 16.04下搭建Web服务器(MySQL+PHP+Apache)
    ESP8266的smartconfig过程(使用esptouch)
    Virtualbox下ubuntu识别USB设备
    红外人体检测
    树莓派跑讯飞语音识别(2麦阵列)
    R16平台中断
    语音识别下的智能家居
  • 原文地址:https://www.cnblogs.com/feitianlee/p/3944800.html
Copyright © 2011-2022 走看看