gpt4 book ai didi

android - 以编程方式将 View 添加到屏幕底部

转载 作者:太空狗 更新时间:2023-10-29 16:12:44 25 4
gpt4 key购买 nike

我想在现有布局的底部添加一个 View ,而不管 Activity 布局文件中定义的 ViewGroup 类型(线性、相对等...)

Activity 布局文件如下所示:

<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:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Ad"
android:layout_centerInParent="true"
android:onClick="showAdd"/>
</RelativeLayout>

这是放置 View 的代码:

ViewGroup rootView = (ViewGroup)((Activity) m_context).findViewById(android.R.id.content);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rootView.addView(adView,lay);

问题是 View 出现在屏幕中间,而不是预期的底部

最佳答案

解决方案是将 View 附加到屏幕的根布局,可以这样实现:

ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);

本质上,根布局是 FrameLayout,将 View 附加到它的底部中心可以像这样实现:

final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM|Gravity.CENTER;
testRoot.addView(dfpBanner, view);

关于android - 以编程方式将 View 添加到屏幕底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40237846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com