【问题标题】:Android Studio: AlignmentAndroid Studio:对齐
【发布时间】:2018-10-12 12:20:18
【问题描述】:

我在 Android Studio 中对齐 webview 和 AdView 时遇到问题:

我想要得到的是底部的广告和整个网络视图。但是,我无法调整 webwiev 的大小以从顶部开始。目前,webview 和网站一样大,如下所示: https://www.dropbox.com/s/bkj00jnck049kqm/Unbenannt.png

这是我的 xml 布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="0dp"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="bottom">

        <WebView
            android:id="@+id/activity_main_webview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/ad_view"
            />

        <com.google.android.gms.ads.AdView
            android:id="@+id/ad_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        />

    </LinearLayout>

</RelativeLayout>

【问题讨论】:

    标签: android android-studio alignment


    【解决方案1】:

    在你的AdView中使用android:layout_alignParentBottom="true"

    示例代码

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="0dp"
        tools:context=".MainActivity"
        tools:ignore="MergeRootFrame">
    
    
        <WebView
            android:id="@+id/activity_main_webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <com.google.android.gms.ads.AdView
            android:id="@+id/ad_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />
    
    
    </RelativeLayout>
    

    【讨论】:

    • 在上述xml代码的webview中添加android:layout_above="@+id/ad_view"
    • 为了获得更好的用户体验,我建议添加它,否则 WebView 和 Google Ads 视图会混淆。
    【解决方案2】:

    请在您当前的代码中进行如下更改

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginTop="0dp"
      tools:context=".MainActivity"
      tools:ignore="MergeRootFrame">
    
    
      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:orientation="vertical">
    
        <WebView
          android:id="@+id/activity_main_webview"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_above="@+id/ad_view"
          />
    
        <com.google.android.gms.ads.AdView
          android:id="@+id/ad_view"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          ads:adSize="SMART_BANNER"
          ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
          />
    
      </RelativeLayout>
    
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2021-03-01
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 2017-09-14
      相关资源
      最近更新 更多