【发布时间】:2023-03-29 23:57:01
【问题描述】:
在我的应用程序中,我有一个AdView 对象和一个LinearLayout(垂直)中的浮动按钮。我希望当用户购买应用内购买 "Remove ads" 时从 xml 中删除 AdView 对象,以便 FAB 按钮取代它。像这样:
没有广告:
我该怎么做? 那是我的xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/myMainRelativeLayout"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar">
</include>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
android:background="?android:selectableItemBackground"
android:id="@+id/recyclerView"
android:layout_below="@+id/tool_bar"
/>
<TextView android:id="@+id/list_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Shopping Items yet"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="100"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<com.software.shell.fab.ActionButton
android:id="@+id/buttonFloat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_gravity="center"
fab:show_animation="@anim/fab_roll_from_down"
fab:hide_animation="@anim/fab_roll_to_down"/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</LinearLayout>
这是我删除广告视图的代码:
mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
if(!mIsRemoveAdds) {
Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}else{
if(mAdView != null) {
mAdView.setVisibility(View.GONE);
}
Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
}
【问题讨论】:
-
你试过 View.GONE 了吗?
-
您可以使用
yourbutton.setVisibility(View.GONE);显示隐藏视图并将布局的宽度和高度设置为0 -
我试过这个,但是当我在已经购买了“删除广告”功能的设备上安装应用程序时,广告视图消失了,但浮动按钮下方仍有空间。
-
如果您使用 View.INVISIBLE,则将保留空间。如果 View.GONE 则不应存在空间。
-
这不是正确的解决方案,而是使用任何 AdView 容器并隐藏该容器,如 FrameLayout 或 LinearLayout 任何东西。
标签: android android-layout android-xml