我正在尝试创建一个图像按钮来关闭来自 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/