缩放 LinearLayouts childs 使其适合屏幕

Scale LinearLayouts childs so they fit on Screen

您好,我正在尝试制作底部带有 AdMob 横幅的游戏菜单。我想在 SurfaceView 上绘制菜单。为了分离横幅和 SurfaceView,我使用了 LinearLayout。问题是 SurfaceView 是全屏的,并将 Banner 推到屏幕外(底部)。无论如何缩放 SurfaceView 以便它不会将 Banner 推到 Screen 之外? (SurfaceView 高度 - 横幅高度)。 这是我的代码:

    linearLayout = new LinearLayout(this);
    linearLayout.setId(Constants.LINLAY_ID);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    contentView = new ContentView(this);

    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    linearLayout.addView(contentView);
    linearLayout.addView(adView);
    setContentView(linearLayout);

` 注意:如果我将其更改为:

linearLayout.addView(adView);
linearLayout.addView(contentView);

横幅显示在顶部,SurfaceView 被向下推了一点。 谢谢你的回答:)

在线性布局中使用布局权重:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <SurfaceView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="4"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>

用您的横幅视图替换 Textview

使用以下动态设置布局权重

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
    LayoutParams.MATCH_PARENT,
    0f,
    1.0f
);
YOUR_VIEW.setLayoutParams(param);

这里第三个参数是布局权重