Android 如何在屏幕上对齐addContentView';安卓的底部
我想问一些关于Android 如何在屏幕上对齐addContentView';安卓的底部,android,android-layout,Android,Android Layout,我想问一些关于addContentView()命令的问题。 我已经创建了一个自定义视图,它是从垂直模式下的线性布局(填充父对象,包裹内容)和一些按钮显式显示的 我的问题:是否可以使用addContentView()命令将自定义视图放置在屏幕底部 我现在使用addContentView(),但我的自定义视图位于屏幕顶部。 我已经尝试在“填充父视图”中更改自定义视图的高度,但是我有一个全屏自定义视图。您必须将线性布局包装到其他布局中。即RelativeLayout,并将其与addContentVie
addContentView()
命令的问题。
我已经创建了一个自定义视图,它是从垂直模式下的线性布局(填充父对象,包裹内容)
和一些按钮显式显示的
我的问题:是否可以使用addContentView()
命令将自定义视图放置在屏幕底部
我现在使用addContentView()
,但我的自定义视图位于屏幕顶部。
我已经尝试在“填充父视图”中更改自定义视图的高度,但是我有一个全屏自定义视图。您必须将线性布局包装到其他布局中。即RelativeLayout,并将其与addContentView一起添加
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/yourLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<!-- Yout content -->
</LinearLayout>
</RelativeLayout>
尝试以下代码
LayoutParams lp =new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity=Gravity.BOTTOM;
addContentView(b, lp);
使用RelativeLayout
也可以实现同样的效果。可以在父布局的底部添加规则以设置视图
谢谢解决方案是将您的自定义视图包装在一个填充整个屏幕的RelativeLayout中,然后添加addContentView。例如,要在屏幕底部添加自定义按钮:
// Fake empty container layout
RelativeLayout lContainerLayout = new RelativeLayout(this);
lContainerLayout.setLayoutParams(new RelativeLayout.LayoutParams( LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT ));
// Custom view
Button mCustomView = new Button(this);
mCustomView.setText("Test");
LayoutParams lButtonParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT );
lButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mCustomView.setLayoutParams(lButtonParams);
lContainerLayout.addView(mCustomView);
// Adding full screen container
addContentView(lContainerLayout, new LayoutParams( LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT ) );
这应该会使它忘记说我不使用xml,而是动态创建布局。在您的代码中,我认为您不能在LinearLayout中使用layout_alignParentBottom=“true”,而是在RelativeLayout中使用。我尝试使用LayoutParams lp=new LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容);lp.重力=重力.底部;但是什么也没发生。这就是为什么布局的高度是包装内容,布局放在屏幕的顶部
// Fake empty container layout
RelativeLayout lContainerLayout = new RelativeLayout(this);
lContainerLayout.setLayoutParams(new RelativeLayout.LayoutParams( LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT ));
// Custom view
Button mCustomView = new Button(this);
mCustomView.setText("Test");
LayoutParams lButtonParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT );
lButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mCustomView.setLayoutParams(lButtonParams);
lContainerLayout.addView(mCustomView);
// Adding full screen container
addContentView(lContainerLayout, new LayoutParams( LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT ) );