以编程方式将视图添加到屏幕底部
Programmatically add a View to the bottom of the screen
我想在现有布局的底部添加一个视图,而不考虑 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>
这是放置视图的代码:
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 出现在屏幕中间,而不是预期的底部
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
您添加的 RelativeLayout 将以整个宽度和高度作为父布局。
由于您的布局参数仅用于子视图,请尝试将其高度设置为 WRAP_CONTENT
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
解决方案是将视图附加到屏幕的根布局,可以这样实现:
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
本质上,根布局是 FrameLayout,将视图附加到它的底部中心可以像这样实现:
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);
我想在现有布局的底部添加一个视图,而不考虑 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>
这是放置视图的代码:
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 出现在屏幕中间,而不是预期的底部
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
您添加的 RelativeLayout 将以整个宽度和高度作为父布局。
由于您的布局参数仅用于子视图,请尝试将其高度设置为 WRAP_CONTENT
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
解决方案是将视图附加到屏幕的根布局,可以这样实现:
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
本质上,根布局是 FrameLayout,将视图附加到它的底部中心可以像这样实现:
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);