【问题标题】:android - how does LayoutParams work?android - LayoutParams 是如何工作的?
【发布时间】:2012-06-27 12:40:25
【问题描述】:

我对此有点困惑。我有一个应用程序,用户可以在其中绘制矩形文本对象。我在 xml 的相对布局中添加了一些文本视图(假设我最多有 3 个文本对象)。对象可以移动和调整大小。我为每个 textView 添加了这段代码

TextView tv=(TextView) findViewById(R.id.text1);            
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
System.out.println(texts.get(i).Sx+ " , "+texts.get(i).Sy+ " , "+ texts.get(i).Lx+ " , "+texts.get(i).Ly);
if(texts.get(i).Sx<=texts.get(i).Lx){
    if(texts.get(i).Sy<=texts.get(i).Ly){                       
        lp.setMargins((int)texts.get(i).Sx, (int)texts.get(i).Sy, (int)texts.get(i).Lx, (int)texts.get(i).Ly);
    } else{
        lp.setMargins((int)texts.get(i).Sx, (int)texts.get(i).Ly, (int)texts.get(i).Lx, (int)texts.get(i).Sy);
    }
} else{
    if(texts.get(i).Sy<=texts.get(i).Ly){
        lp.setMargins((int)texts.get(i).Lx, (int)texts.get(i).Sy, (int)texts.get(i).Sx, (int)texts.get(i).Ly);
    } else{
        lp.setMargins((int)texts.get(i).Lx, (int)texts.get(i).Ly, (int)texts.get(i).Sx, (int)texts.get(i).Sy);
    }
}           
tv.setLayoutParams(lp);
tv.setWidth((int)(texts.get(i).width));
tv.setHeight((int)(texts.get(i).height));
tv.setTextSize(texts.get(i).textSize);
tv.setText(text.text);
tv.setTextColor(Color.WHITE);
tv.requestLayout();

Sx, Sy 是对象的起始坐标(以像素为单位),Lx, Ly 是结束坐标。

在xml中我添加了这个:

<RelativeLayout 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" 
    android:id="@+id/texts" >

    <TextView 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent" 
        android:id="@+id/text1" />

    <TextView 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent" 
        android:id="@+id/text2" />

    <TextView 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent" 
        android:id="@+id/text3" />              

</RelativeLayout>

这似乎可以将文本放置在正确的位置。但是textview的宽度和高度似乎不对。这是以像素为单位设置边距的问题吗?一些启示现在会非常有用

【问题讨论】:

  • 如果你的@text1 垂直和水平地填充父级,那么其他的就没有任何东西了
  • 这是一个相对布局,所以他们可以一个在另一个之上,不是吗?问题是,只有 text1 它也有问题

标签: android android-relativelayout textview layoutparams


【解决方案1】:

您首先将宽度/高度设置为 FILL_PARENT,然后用 px 覆盖它?

试试这样的

View temp = this.findViewById(recent);
RelativeLayout.LayoutParams relativeParams = (LayoutParams) temp.getLayoutParams();

relativeParams.setMargins(0, 0, 0, 50); // llp.setMargins(left, top, right, bottom);
relativeParams.width = 123;
relativeParams.height = 123;
temp.setLayoutParams(relativeParams);

【讨论】:

  • 上述代码中的“最近”是什么意思?我的文本视图?还是根相对布局?
  • 你的 textView,基本上任何 R.id.something
【解决方案2】:

LayoutParams 用于动态对齐视图。您可以将规则添加到您的参数中,例如下方、上方、底部、顶部、对齐、边距和所有(与通过 xml 执行的操作相同),最后您可以将此布局参数设置到您的视图中。例如:

    // Set the params eg. for a button.

    RelativeLayout.LayoutParams button_params = new RelativeLayout.LayoutParams(RelativeLayout
        .LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    button_params.addRule(RelativeLayout.LEFT_OF,
                          any_view.getId());
    button_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
                          RelativeLayout.TRUE);
    button_params.bottomMargin = screen_height / 15;
    button_params.rightMargin = 20;
    button.setLayoutParams(button_params);

【讨论】:

  • 但这和我用 lp.setMargin 方法做的不一样吗?我希望我的 textView 完全适合用户绘制的框。我无法预测盒子在哪里,但我有 coos (Sx,Sy,Lx,Ly)。我也试过 lp.topMargin=(int)texts.get(i).Sy; lp.bottomMargin=(int)texts.get(i).Ly; lp.leftMargin=(int)texts.get(i).Sx; lp.rightMargin=(int)texts.get(i).Lx;但问题依然存在
猜你喜欢
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 2011-09-04
相关资源
最近更新 更多