Android,RelativeLayout和动态添加元素

时间:2012-01-22 05:28:13

标签: android

我有一个应用,我想添加一个带广告的免费版本,但我想对我的代码进行微小的更改。所以在我的布局中我做到了这一点:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/mainLayout"
android:layout_width="fill_parent" android:layout_height="0dip"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">

<FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/adFrameLayout" android:background="@android:color/white">

</FrameLayout>
<LinearLayout android:id="@+id/buttonGrp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"
    android:layout_below="@id/adFrameLayout" android:orientation="horizontal">
    <Button android:id="@+id/normalGameBtn" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="@string/normalGame"/>
    <Button android:id="@+id/shareBtn" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="@string/shareButton"/>
</LinearLayout>

<RadioGroup android:id="@+id/aiRadioGrp" android:layout_below="@id/buttonGrp"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:checkedButton="@+id/normalAI" android:layout_gravity="center_horizontal">
     ...
</RadioGroup>
<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/aiRadioGrp">
    <TextView android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:text="@string/description" android:textSize="16dip"
        />
</ScrollView>

所以我想要的是位于顶部的FrameLayout(adFrameLayout),如果它是免费版本,我希望它显示广告,如果支付,则尺寸应保持为0。

因此,在base(pro)代码中,我使用Guice注入一个在onCreate中执行额外处理的对象。在专业版中,代码什么都不做,在我这样做的免费版本中:

@Override
public void accept(Activity activity) {
    FrameLayout frame = (FrameLayout) activity.findViewById(R.id.adFrameLayout);
    frame.getLayoutParams().height = 20;
    frame.invalidate();
}

这只是一个确保它可以工作的例子,它没有,FrameLayout保持大小为0.我怎样才能让它在代码中动态调整大小?当然,如果我在xml中指定20px它工作正常,但我猜在付费版本中我试图将大小设置为0也不起作用。我究竟做错了什么?

BTW我在执行setContentView(R.id.main)

之后设置了大小

1 个答案:

答案 0 :(得分:0)

Ugghh,它涉及AndroidAnnotations。由于某些原因,它没有像onCreate方法那样获取布局更改,我不得不将它放在postOnCreate方法中。这可能已经在某个地方进行了记录,但我错过了它。继续

相关问题