问题描述
我正在尝试从RelativeLayout迁移到ConstraintLayout,但在将AdMob AdView添加到底部以及其上其余内容时遇到了问题.例如,使用RelativeLayout就这么简单.您只需将android:layout_alignParentBottom="true"
放在AdView上,将android:layout_above="@+id/adView"
放在包含这些内容的视图上.
我试图了解如何使用ConstraintLayout而不是RelativeLayout将示例代码和这两行代码迁移到等效代码.
请,有人可以帮助我进行此迁移吗?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/adView"/>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
ads:adSize="BANNER"
ads:adUnitId="@string/ad_id_banner">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
我在我想要的adView上方的内容上使用app:layout_constraintBottom_toTopOf="@+id/adView"
并在adView上的app:layout_constraintBottom_toBottomOf="parent"
上使用了app:layout_constraintBottom_toTopOf="@+id/adView"
,但由于该内容不在adView上方,所以无法正常工作.而且adView不会居中.非常令人沮丧.
首先,我想谈一谈ConstraintLayout
特有的两种行为,当您初次使用它们时并不一定很明显./p>
match_parent
不支持
此详细信息在开发人员指南的单行中包含: https ://developer.android.com/training/constraint-layout/index.html
要获得匹配父级"行为,请将0dp
维度与视图的两个对应边的约束条件结合起来:
android:layout_width="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
仅在具有相应约束的情况下强制执行保证金
仅添加android:layout_marginTop
不会执行任何操作,除非该视图也具有最高约束,例如app:layout_constraintTop_toTopOf
或app:layout_constraintTop_toBottomOf
.
您的特定问题
这是更新的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/adView"/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:adSize="BANNER"
app:adUnitId="@string/ad_id_banner"/>
</android.support.constraint.ConstraintLayout>
为实现AdView
的水平居中,我们将左右边缘限制为父对象的左右边缘.这样将从两侧均匀地拉" AdView
并将其居中.为了使其固定在屏幕底部,我们将其底部边缘限制在父级的底部.
对于LinearLayout
,首先我们将其尺寸均更改为0dp
.这将使约束条件定义其大小.然后,将上边缘,左边缘和右边缘约束到父对象的上边缘,左边缘和右边缘.我们将底部边缘限制为AdView
的顶部边缘.这会导致它水平填充屏幕,并填充AdView
未使用的所有垂直空间.
最后,我们将AdView
的顶部边距更改为LinearLayout
的底部边距.这是因为LinearLayout
的底部边缘被限制在AdView
的顶部边缘(但是AdView
的顶部边缘没有被限制在LinearLayout
的底部边缘,所以上边距无效).
I'm trying to migrate from RelativeLayout to ConstraintLayout but I'm having problems adding the AdMob AdView to the bottom and the rest of the content above it. For example, using RelativeLayout was that simple. You only need to put android:layout_alignParentBottom="true"
on the AdView and android:layout_above="@+id/adView"
on the view which is containing the stuff.
I'm trying to understand how to migrate this sample code and these two lines of code to the equivalent using a ConstraintLayout instead of a RelativeLayout.
Please, can someone help me with this migration?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/adView"/>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
ads:adSize="BANNER"
ads:adUnitId="@string/ad_id_banner">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
I tested using app:layout_constraintBottom_toTopOf="@+id/adView"
on the stuff i want above the adView and app:layout_constraintBottom_toBottomOf="parent"
on the adView but it's not working because the stuff is not above the adView it's behind. And Also the adView is not centered. It's very frustrating.
First, I want to talk about two pieces of behavior that are unique to ConstraintLayout
and that aren't necessarily obvious when you first pick it up.
match_parent
is not supported
This detail is tucked away in a one-liner in the developer guide: https://developer.android.com/training/constraint-layout/index.html
To get "match parent" behavior, combine a 0dp
dimension with constraints for both corresponding edges of your view:
android:layout_width="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
Margins are only enforced if they have a corresponding constraint
Just adding android:layout_marginTop
will not do anything unless that view also has a top constraint, like app:layout_constraintTop_toTopOf
or app:layout_constraintTop_toBottomOf
.
Your specific issue
Here's the updated layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/adView"/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:adSize="BANNER"
app:adUnitId="@string/ad_id_banner"/>
</android.support.constraint.ConstraintLayout>
To achieve the horizontal centering of the AdView
, we constrain the left and right edges to the parent's left and right edges. This "pulls" the AdView
equally from each side, centering it. To get it to stick to the bottom of the screen, we constrain its bottom edge to the bottom of the parent.
As for the LinearLayout
, first we change its dimensions both to 0dp
. This will let the constraints define its size. Then we constrain the top, left, and right edges to the top, left, and right edges of the parent. We constrain the bottom edge to the top edge of the AdView
. This causes it to fill the screen horizontally, and fill all of the vertical space the AdView
isn't using.
Finally, we change the top margin on the AdView
into a bottom margin on the LinearLayout
. This is because the LinearLayout
's bottom edge is constrained to the AdView
's top edge (but the AdView
's top edge isn't constrained to the LinearLayout
's bottom edge, so top margin would have no effect).
这篇关于如何在ConstraintLayout中将AdView放在其他内容的底部和下方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!