2011-08-17 38 views
2

我試圖創建一個imagebutton關閉來自admob的廣告。 我已經在可見性屬性設置爲「不可見」的xml中創建了按鈕,然後在Java中,我在收到廣告時將其設置爲「可見」,但該按鈕永遠不可見。 如果我將它設置爲在XML上「可見」硬編碼,它通常會顯示在屏幕上。如何使ImageButton可見/ invisibl

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(); 
    } 
+0

是U試圖把它的廣告的頂部? (重疊?) – Ronnie

+0

你能改一下你的問題 - 這句話是模糊的」 ......然後在我的Java將它設置爲‘可見’當接收到廣告,但從未按鈕變得可見。如果我將它設置爲「可見」,它通常會顯示在屏幕上。'這是否意味着它會在您將其設置爲可見時出現或不出現 - 您都說過了! – Martyn

+0

請顯示按鈕的xml代碼的所有參數。 – Gangnus

回答

1

您應該在admob佈局之後添加圖像按鈕。
這樣的:

<RelativeLayout ...> 
    <Admob..../> 
    <ImageButton..../> 
</RelativeLayout> 
+0

這篇幫助? .. – Ronnie

5

button.setVisibility(View.VISIBLE); - 絕對正確。你有沒有檢查應用程序是否真的到了這一行?在我看來,它沒有。

+0

是的,我已經試過了debug模式和代碼達到 –