如何在android中定义视图的宽度和高度

时间:2018-02-09 11:19:20

标签: java android android-studio

我通过drawable canvas实现了android游戏,我希望在我的游戏中使用横幅广告。但现在它显示横幅,但无法看到我的整个游戏,我只看到白色屏幕。如何解决这个问题?

This is my game play which is made up of drawable canvas in android

after changing in this code my game play shows white screen and its show only banner ad

GameActivity.java

public class GameActivity extends Activity {
    private AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        //GameView gView = new GameView(this);
        GameView gView = new GameView(this);
        gView.setKeepScreenOn(true);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        MobileAds.initialize(getApplicationContext(), "ca-app-pub-0664573200302260/332653322424");
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

//Additionally to adjust the position to Bottom
        layout.setGravity(Gravity.BOTTOM);



// Create a banner ad
        mAdView = new AdView(this);
        mAdView.setVisibility(View.VISIBLE);
        mAdView.setAdSize(AdSize.BANNER);
        mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

// Create an ad request.
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();


// Optionally populate the ad request builder.
        adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

// Add the AdView to the view hierarchy.
        layout.addView(mAdView);

// Start loading the ad.
        mAdView.loadAd(adRequestBuilder.build());

        setContentView(layout);


    }
}

1 个答案:

答案 0 :(得分:0)

Your_View.getLayoutParams().height  = getResources().getDimensionPixelSize(R.dimen.Whatever_Height);

Your_View.getLayoutParams().width  = getResources().getDimensionPixelSize(R.dimen.Whatever_Height);

在dimens.xml中添加所需的高度和宽度,如:

<dimen name="dimension10">10dp</dimen>

所以你可以在R.dimen.dimension10

中使用它
相关问题