0

Here is my xmls (there are basically two views: a linear [B] inside a relative [A])

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/A"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content" >

<LinearLayout
android:id="@+id/B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>

</<RelativeLayout>

I need to set the RelativeLayout's params but I have no idea what class it should belonged to (since it is parentless).

SomethingLayout.LayoutParams p = new SomethingLayout.LayoutParams(w*10, h);
A.setLayoutParams(p);

It would be great if someone can tell me how this can be done.


The reason why I need to do that is because I want to set the width of B as 10 times of the screen width, namely w*10:

RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams(w*10, h);
B.setLayoutParams(para);

When I animate B,

ObjectAnimator animation2 = ObjectAnimator.ofFloat(B, "x", -w)

it shows that the width of B is w rather than w*10, as if A, who is being the parent view, is limiting its width.

And therefore I figured out that setting A's params can solve the problem. But I have no idea how to do that...

4

2 回答 2

0

如果您调用方法“setContentView(R.layout.A);” 在你的活动中。A的父母类型可能是Framelayout。

您可以编写如下代码:

setContentView(R.layout.A);
ViewGroup p = (ViewGroup) A.getParent();
if(p instanceof FrameLayout){
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(w*10, h);
    A.setLayoutParams(p);
}

我是中国人,我的英语不是很好,所以如果我犯了任何语法错误,请原谅我。

于 2013-05-22T03:19:43.840 回答
0

看看这样的东西是否适合你

RelativeLayout.LayoutParams para = B.getLayoutParams();
para.width = w*10;
para.height = h;
B.setLayoutParams(para);
于 2013-05-22T03:24:18.413 回答