- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试从 RelativeLayout 迁移到 ConstraintLayout,但我在将 AdMob AdView 添加到底部以及将其余内容添加到其上方时遇到问题。例如,使用 RelativeLayout 就是这么简单。您只需要将 android:layout_alignParentBottom="true"
放在 AdView 上,将 android:layout_above="@+id/adView"
放在包含内容的 View 上。
我试图了解如何使用 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"
但它不起作用,因为这些东西不在它后面的 adView 之上。而且 adView 也没有居中。这非常令人沮丧。
最佳答案
首先,我想谈谈 ConstraintLayout
独有的两个行为,当您第一次接触它时,它们不一定很明显。
match_parent
不受支持
此详细信息隐藏在开发人员指南的一行中:https://developer.android.com/training/constraint-layout/index.html
Note: You cannot use
match_parent
for any view in aConstraintLayout
. Instead use "match constraints" (0dp
).
要获得“匹配父级”行为,请将 0dp
维度与 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"
边距只有在有相应约束的情况下才会强制执行
仅添加 android:layout_marginTop
不会做任何事情,除非该 View 也有顶部约束,例如 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
的底部边缘,因此顶部边距不会有任何影响)。
关于android - 如何将 AdView 放在 ConstraintLayout 的底部和其他东西下面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49137449/
如何在不使用垫片的情况下将 View 定位在底部。我知道我可以在 VStack 中放置一个间隔器和我的 View 来实现它,但我不想使用一个间隔器,因为我不希望我的 View 占据所有的垂直空间。使用
我想让文本 float 到现有标签的右下角。 我仍在使用旧学校表(编辑现有代码)。我的代码是这样的:
我有一个包含文本的表格单元格,但我怎样才能使文本齐平到单元格的顶部或底部(上面没有填充)?我试过垂直对齐,但它仍然没有到达顶部“边缘”。 我的代码 3.2325
我想用 javascript 打印一个特殊的页面 div。 function printDiv(divName) { var printContents = document
我需要检查元素是否距离页面底部 x 像素,以动态加载新内容。目前,即使栏位于底部,scrollTop 和高度也不匹配。 jquery 是允许的,虽然基本的 javascript 会更有帮助。 最佳答案
我正在用 pygame 重新制作 flappy bird,但主题是星球大战。我已经完成了游戏的美术和一般格式设置,但现在我需要调整细节。我一直在改变数字,试图让光剑完全到达屏幕的顶部和底部,因为目前有
http://pastehtml.com/view/bfzerlo1m.html 如何将红色框放在橙色 div 底部的 CENTER + 中? 红框的高度和宽度都是动态的,每个框都不同.. (它需要在
我正在研究和测试表格。到目前为止,我成功地使用 following fiddle 将选择框列表中的项目一项一项地向上和向下移动。 . 代码实例(向上移动): function moveUp() { $
在设计 IOS 应用程序时,我可以在页脚放置“后退”按钮功能吗? 最佳答案 我认为指南中没有任何内容禁止这样做,但是您的潜在用户习惯于将后退按钮放在左上角,因此除非您有充分的理由将后退按钮放在底部,并
你可以只在顶部/底部而不是所有(T、B、L、R)设置单元格填充或间距吗? 最佳答案 CSS? td { padding-top: 2px; padding-bottom: 2px; } 关于H
我正在为我的 React 应用程序使用无限滚动,并具有检测我何时恰好位于页面底部的功能: const [isFetching, setIsFetching] = useState(false); //
所以我有一个页面,其中有一个类似聊天的 div,里面充满了文本。它具有固定的高度和宽度(由 css 定义)。 我需要它在每次更新时滚动到底部,这是我到目前为止使用的 JS: $("#div1").an
我遇到了与此处描述的相同的问题:UIWebView doesn't scroll to the bottom of the webpage loaded/created (不幸的是没有人回答) 我有一
我有一个溢出设置为滚动的 div,它本质上是逐行从文件中流式传输数据。我想在流溢出时自动滚动到 div 的底部,但不使用“单击此处滚动到底部”按钮。 我已经知道 scrollTop = scrollH
我正在 Android studio 中构建一个应用程序,但遇到了一些问题。我在 main_activity.xml 中有一个 ImageView
我有一个扩展 Jpanel 的类,里面有一个动画。我有两个操作按钮可以停止和启动它,但是我需要这些按钮出现在 Jpanel 的底部。我已经尝试过: add(go,BOTTOM_ALIGNMENT);
嗨,我正在为ios设备(ipad)开发phonegap / cordova项目。该应用程序通过蓝牙键盘接收输入文本(因为我不想在屏幕上显示键盘)。到目前为止,应用程序可以按预期接收输入。但是关于外观,
我想在底部放置一个页脚。 出于某种原因,它会像这样出现。 在 index.html 中,我有:
我得到了一个带有内联编辑功能的 jqgrid,并且可以添加新行。 目前,新行以“编辑”模式显示在网格顶部。我想要的是将新行添加到网格底部,因为我的“添加新行”按钮位于自定义底部分页器中... 有人知道
在 Swift 3 中,我以编程方式创建了一个(底部)工具栏,其中包含自定义按钮,中间用灵活的垫片分隔自定义按钮,将一个(“上一个”)推到左边缘,另一个(“下一个”)到 View 的右边缘。但我无法显
我是一名优秀的程序员,十分优秀!