【问题标题】:Android: How to Remove an object from xml programaticallyAndroid:如何以编程方式从 xml 中删除对象
【发布时间】: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


【解决方案1】:

要删除您可以使用的任何视图,其中ll 是您的父线性布局

ll.removeView(view)// to remove particular view
ll.removeViewAt(position);// to remove view from particular position

或者,您也可以将您的 adView 的 visibility 设置为 GONE,从而将其位置给按钮。

adButton.setVisibility(View.GONE);  

编辑:
在查看您的 XML 之后,我认为它的重要性正在造成问题。
您应该将 floatingButton layout_weight 设置为 100 以隐藏 adView

floatingButton.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 100f));

编辑:
我建议不要使用权重,因为您不需要它们。

【讨论】:

  • 我试过这个,但是当我在已经购买了“删除广告”功能的设备上安装应用程序时,广告视图消失了,但浮动按钮下方仍有空间。
  • 我认为它的 layout_weight 有问题,您还应该在删除 adView 时将 floatingButton 的权重设置为 100 - 请参阅stackoverflow.com/a/3224365/3828908 的权重
  • 我还是有问题,将按钮的 layout_weight 设置为 100 :/
  • 什么问题,代码不执行还是什么?用 100.0f 试过了吗?
  • 不,它只是不起作用,空间在那里。也许我为 LayoutParams 导入了错误的类,因为 android studio 为我提供了很多选项。我选择了 android.view.ViewGroup.LayoutParams
【解决方案2】:

如果要动态判断是否显示,在xml中添加视图是不现实的。如果您仍想添加以添加到 xml,您可以使其不可见或分离/删除视图。否则,请勿在 Xml 中添加 AdView。我处理这种情况如下:

        adView = new MoPubView(this.getApplicationContext());
        adView.setAdUnitId(getString(R.string.mopub_banner_adunitid));
        adView.refreshDrawableState();
        adView.setBannerAdListener(this);
        if(!Prefs.getUserPreferenceBooleanValue(Prefs.Key.REMOVE_ADS_PAID, this)) {
              frameLayout.addView(adView, bannerViewLayoutParams);      
              adView.loadAd();

        }

以编程方式在您的代码中添加广告视图。在共享首选项中存储一个标志并根据需要设置它以显示或删除广告。当活动加载时,读取标志并确定是否将广告视图添加到布局中。

对于按钮的大小,您可以将其发送为相对于可用空间或根据标志的值调整大小。

【讨论】:

  • 这是一种有趣的方法,我一定会去看看。谢谢!
  • 补充以上内容,您甚至可以通过将整个代码放在 if 块中来选择根本不实例化对象。这将使用更少的内存。差别不大,但更好。我这样做还有其他原因。
【解决方案3】:

假设 youViewName 是您要删除的视图。 如果它是 LinearLayout 使用下面的代码,否则如果它是 RelativeLayout 将下面代码中的 LinearLayout 替换为 RelativeLayout

((LinearLayout) youViewName.getParent()).removeView(youViewName);

在你的情况下,下面的行应该可以工作

((LinearLayout) mAdView.getParent()).removeView(mAdView);

【讨论】:

    猜你喜欢
    • 2011-05-23
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 2020-10-24
    • 2014-07-22
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多