2014-08-29 59 views
1

我试图让我的广告旗帜,以我的屏幕的底部,但它不断显示在屏幕的中间了,这里是它的外观图片,如:http://i.gyazo.com/8e5e79219bb7e03a757778b8c13d9cd6.png如何将我的广告横幅放到我的应用程序底部?

这是我的java文件:

public class MainActivity extends Activity { 

ImageButton btnSwitch; 

private Camera camera; 
private boolean isFlashOn; 
private boolean hasFlash; 
Parameters params; 
MediaPlayer mp; 

private AdView adView; 

/* Your ad unit id. Replace with your actual ad unit id. */ 
private static final String AD_UNIT_ID = "ca-app-pub-3642697202265316/5249422388"; 

@SuppressWarnings("deprecation") 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Create an ad. 
    adView = new AdView(this); 
    adView.setAdSize(AdSize.BANNER); 
    adView.setAdUnitId(AD_UNIT_ID); 

    // Add the AdView to the view hierarchy. The view will have no size 
    // until the ad is loaded. 
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout); 
    layout.addView(adView); 



    // Create an ad request. Check logcat output for the hashed device ID to 
    // get test ads on a physical device. 
    AdRequest adRequest = new AdRequest.Builder() 
     .build(); 

    // Start loading the ad in the background. 
    adView.loadAd(adRequest); 


    // flash switch button 
    btnSwitch = (ImageButton) findViewById(R.id.btnSwitch); 


    // First check if device is supporting flashlight or not   
    hasFlash = getApplicationContext().getPackageManager() 
      .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); 

    if (!hasFlash) { 
     // device doesn't support flash 
     // Show alert message and close the application 
     AlertDialog alert = new AlertDialog.Builder(MainActivity.this) 
       .create(); 
     alert.setTitle("Error"); 
     alert.setMessage("Sorry, your device doesn't support flash light!"); 
     alert.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // closing the application 
       finish(); 
      } 
     }); 
     alert.show(); 

     return; 


    } 

    // get the camera 
    getCamera(); 

    // displaying button image 
    toggleButtonImage(); 


    // Switch button click event to toggle flash on/off 
    btnSwitch.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if (isFlashOn) { 
       // turn off flash 
       turnOffFlash(); 
      } else { 
       // turn on flash 
       turnOnFlash(); 
      } 
     } 
    }); 
} 



// Get the camera 
private void getCamera() { 
    if (camera == null) { 
     try { 
      camera = Camera.open(); 
      params = camera.getParameters(); 
     } catch (RuntimeException e) { 
      Log.e("Camera Error. Failed to Open. Error: ", e.getMessage()); 
     } 
    } 
} 

// Turning On flash 
private void turnOnFlash() { 
    if (!isFlashOn) { 
     if (camera == null || params == null) { 
      return; 
     } 
     // play sound 
     playSound(); 

     params = camera.getParameters(); 
     params.setFlashMode(Parameters.FLASH_MODE_TORCH); 
     camera.setParameters(params); 
     camera.startPreview(); 
     isFlashOn = true; 

     // changing button/switch image 
     toggleButtonImage(); 
    } 

} 

// Turning Off flash 
private void turnOffFlash() { 
    if (isFlashOn) { 
     if (camera == null || params == null) { 
      return; 
     } 
     // play sound 
     playSound(); 

     params = camera.getParameters(); 
     params.setFlashMode(Parameters.FLASH_MODE_OFF); 
     camera.setParameters(params); 
     camera.stopPreview(); 
     isFlashOn = false; 

     // changing button/switch image 
     toggleButtonImage(); 
    } 
} 


// Playing sound 
// will play button toggle sound on flash on/off 
private void playSound(){ 
    if(isFlashOn){ 
     mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off); 
    }else{ 
     mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on); 
    } 
    mp.setOnCompletionListener(new OnCompletionListener() { 

     @Override 
     public void onCompletion(MediaPlayer mp) { 
      // TODO Auto-generated method stub 
      mp.release(); 
     } 
    }); 
    mp.start(); 
} 

/* 
* Toggle switch button images 
* changing image states to on/off 
* */ 
private void toggleButtonImage(){ 
    if(isFlashOn){ 
     btnSwitch.setImageResource(R.drawable.btn_switch_on); 
    }else{ 
     btnSwitch.setImageResource(R.drawable.btn_switch_off); 
    } 
} 

@Override 
protected void onDestroy() { 
    if (adView != null) { 
      adView.destroy(); 
     } 
    super.onDestroy(); 
} 

@Override 
protected void onPause() { 
    if (adView != null) { 
      adView.pause(); 
     } 
    super.onPause(); 
    // on pause turn off the flash 
    turnOffFlash(); 
} 

@Override 
protected void onRestart() { 
    super.onRestart(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    // on resume turn on the flash 
    if(hasFlash) 
     turnOnFlash(); 
    if (adView != null) { 
     adView.resume(); 
     } 
} 

@Override 
protected void onStart() { 
    super.onStart(); 

    // on starting the app get the camera params 
    getCamera(); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 

    // on stop release the camera 
    if (camera != null) { 
     camera.release(); 
     camera = null; 
    } 

    ActionBar actionBar = getActionBar(); //or getActionBar() 
    actionBar.hide(); 

} 

private boolean doubleBackToExitPressedOnce = false; 

protected void onResume1() { 
    super.onResume(); 
    // .... other stuff in my onResume .... 
    this.doubleBackToExitPressedOnce = false; 
} 

@Override 
public void onBackPressed() { 
    if (doubleBackToExitPressedOnce) { 
     super.onBackPressed(); 
     return; 
    } 
    this.doubleBackToExitPressedOnce = true; 
    Toast.makeText(this, "Press Back Again To Exit", Toast.LENGTH_SHORT).show(); 

} 

任何帮助?谢谢。

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearLayout" 
android:orientation="vertical" 
android:keepScreenOn = "true" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/radial_background" 
tools:context=".MainActivity" 
xmlns:ads="http://schemas.android.com/apk/res-auto" > 

<ImageButton 
    android:id="@+id/btnSwitch" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="100dip" 
    android:src="@drawable/btn_switch_on" 
    android:background="@null" 
    android:contentDescription="@null"/> 


    </LinearLayout> 
+0

后'activity_main' XML – 2014-08-29 01:54:07

+0

它只是添加到我的职务。 – kenslee123 2014-08-29 02:01:41

回答

0

你可以改变你线性布局相对布局。如果你改变你可以从相对布局例如:“对齐母公司中心”或“对齐母公司底部”属性将解决您的设计视图的一些属性。希望得到帮助。

为您的代码:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)adView.getLayoutParams(); 
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
adView.setLayoutParams(params);