我在Xamarin中这样做,因此套管和方法名称会略有偏差。
我有一个包含广告的RelativeLayout,位于底部。不幸的是,广告阻止了可播放地图的一部分,所以当玩家靠近底部移动时,我试图将其移动到顶部。我使用以下代码初始化横幅:
_banner = new RelativeLayout(this);
_lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
_lp.AddRule(LayoutRules.AlignParentBottom);
_lp.AddRule(LayoutRules.AlignParentLeft);
AddContentView(_banner, _lp);
我现在正试图将它移到顶部,但我失败了。我已经尝试删除并重新添加它,但这没有任何作用。
var lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
((FrameLayout)(_banner.Parent)).RemoveView(_banner);
lp.AddRule(LayoutRules.AlignParentTop);
lp.AddRule(LayoutRules.AlignParentLeft);
AddContentView(_banner, lp);
我也尝试过设置LayoutParameters,但是会引发异常。我做错了什么?
谢谢。
-Nick
答案 0 :(得分:1)
从我的代码中我收集到的是在FrameLayout中嵌套RelativeLayout(包含广告)。您的RelativeLayout仅在宽度上匹配您的父级,而不是高度。 因此,RelativeLayout与FrameLayout的大小不同。
因此,您的广告会触及RelativeLayout的顶部和底部。并且RelativeLayout始终与FrameLayout的顶部对齐。
要解决此问题,您有三种选择:
您的代码正在有效地执行:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:alignParentBottom="true"
android:background="#FF0000"
android:layout_width="match_parent"
android:layout_height="240dp"
/>
</FrameLayout>
但是对于工作的东西,你需要使用android:layout_gravity,这是一个FrameLayout.LayoutParameter。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_gravity="bottom"
android:background="#FF0000"
android:layout_width="match_parent"
android:layout_height="240dp"
/>
</FrameLayout>
只需将两个部分粘贴到layout.xml中,然后使用Android Designer查看差异。