android - 如何使图像按钮可见/不可见

标签 android eclipse android-layout admob imagebutton

我正在尝试创建一个图像按钮来关闭来自 admob 的广告。 我在 xml 中创建了按钮,并将可见性属性设置为“不可见”,然后在 java 上,当收到广告时将其设置为“可见”,但该按钮永远不会变得可见。 如果我将其设置为在 XML 上硬编码的“可见”,它会正常显示在屏幕上。

Admob 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout_main">
    <ImageButton        
        android:id="@+id/close_ad"
        android:visibility="invisible"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="-36dip"
        android:layout_marginTop="12dip"
        android:background="@drawable/btn_close_ad" />  
</RelativeLayout>

添加广告:

private void addAd() {
    rl = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.admob, null);
    rl.setGravity(position);
    activity.addContentView(rl, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    AdView admobView = new AdView(activity, AdSize.BANNER, Preferences.getAdmobKey());
    admobView.loadAd(new AdRequest());
    [ ... ]
    admobView.setAdListener(new AdListener() {
    @Override
    public void onReceiveAd(Ad ad) {
        Log.v("JeraAdmob", "onReceiveAd");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                createCloseButton();
            }
        }, AD_SHOW_CLOSE);
    }
    });
}

创建按钮来关闭广告:

private void createCloseButton() {
        ImageButton button = (ImageButton) rl.findViewById(R.id.close_ad);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rl.removeAllViews();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        addAd();
                        rl.bringToFront();
                    }
                }, AD_RELOAD);
            }
        });
        button.setVisibility(View.VISIBLE);
        button.bringToFront();
    }

最佳答案

button.setVisibility(View.VISIBLE); - 绝对正确。你检查过应用程序是否真的到达了这条线吗?在我看来,事实并非如此。

关于android - 如何使图像按钮可见/不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7095145/

相关文章:

java - 如何在android中的水平 ScrollView 中添加多个 child

android - LinearLayout 中的 ImageViews - 弄乱比例

Textview 上的 Android 内存泄漏 - LeakCanary(泄漏可以忽略)

php - eclipse pdt 中的代码完成和工厂模式

android - Android中如何将json对象转换成字符串

Eclipse 4 朱诺主题

java.lang.OutOfMemoryError : PermGen space in embeded jetty 错误

android - 如何在 Android 中将 AutoCompleteTextView 下拉背景设置为透明?

android - 用于数字液晶显示器的 Firebase MLkit 文本识别

android - 将 XML WebView 转换为 @Composable