【问题标题】:Android RelativeLayout params ignoredAndroid RelativeLayout 参数被忽略
【发布时间】:2013-04-02 09:31:58
【问题描述】:

我遇到了一个奇怪的问题: 我有一个从 RelativeLayout 扩展的类。

然后我想像这样添加两个视图:

mButtonTip = new ButtonTip(context, attrs);
mToolTip = new ToolTip(context, attrs);

RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_BOTTOM, mToolTip.getId());

addView(mToolTip, params1);
addView(mButtonTip, params2);

不幸的是,mToolTip 视图已正确放置,但 mButtonTip 不会从布局的左下方移动。

你知道为什么 mButtonTip 视图会忽略我刚刚设置的布局参数吗?

【问题讨论】:

    标签: android android-relativelayout android-custom-view


    【解决方案1】:

    好的,所以对于那些可能遇到相同问题的人,我找到了解决方案:

    我需要像这样明确设置不同视图的 id:

    mButtonTip = new ButtonTip(context, attrs);
    mButtonTip.setId(1);
    
    mToolTip = new ToolTip(context, attrs);
    mToolTip.setId(2);
    
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    
    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params2.addRule(RelativeLayout.ALIGN_BOTTOM, mToolTip.getId());
    
    addView(mToolTip, params1);
    addView(mButtonTip, params2);
    

    【讨论】:

      猜你喜欢
      • 2017-09-05
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多