如何使 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();
}
I'm trying to create an imagebutton to close an ad from admob.
I've created the button in the xml with the visibility property set to "invisible" and then on java I set it to "visible" when the ad is received, but the button never become visible.
If I set it to "visible" hardcoded on the XML it appears on the screen normally.
Admob layout:
<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>
Add ad:
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);
}
});
}
Create button to close the ad:
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按钮.setVisibility(View.VISIBLE); - 绝对正确。你检查过应用程序是否真的到达了这条线吗?在我看来,事实并非如此。
button.setVisibility(View.VISIBLE); - is absolutely correct. Have you checked if the app really got to this line? It seems to me that it did not.
您应该在 admob 布局之后添加图像按钮。
像这样:
You should add the image button after the admob layout.
like this: