eclipse 如何使 ImageButton 可见/不可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7095145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make an ImageButton visible/invisibl
提问by Adriano Bacha
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 的广告。我在 xml 中创建了按钮,并将可见性属性设置为“不可见”,然后在 java 上我在收到广告时将其设置为“可见”,但按钮永远不会变得可见。如果我将它设置为“可见”硬编码在 XML 上,它会正常显示在屏幕上。
Admob layout:
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>
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();
}
采纳答案by Ronnie
You should add the image button after the admob layout.
like this:
您应该在 admob 布局之后添加图像按钮。
像这样:
<RelativeLayout ...>
<Admob..../>
<ImageButton..../>
</RelativeLayout>
回答by Gangnus
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.
button.setVisibility(View.VISIBLE); ——完全正确。您是否检查过该应用程序是否真的到达了这一行?在我看来,它没有。