How to align addContentView at the screen's bottom in Android
I would like to ask something about the addContentView()
command.
I have created a custom view apparted from a LinearLayout(fill parent, wrap content)
in vertical mode and some buttons.
My question: Is it possible 开发者_运维问答to place my custom view at the bottom of the screen by using the addContentView()
command?
I now use addContentView()
but my custom view is placed at the top of my screen.
I have already tried to change the height of the custom view in fill parent but then I have a full screen custom view.
Solution is to wrap your custom view in a RelativeLayout that fills whole screen and then add it with addContentView. For example to add a custom Button on the bottom of the screen:
// 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 ) );
This should make it
Try with the folowing code
LayoutParams lp =new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity=Gravity.BOTTOM;
addContentView(b, lp);
Using RelativeLayout
also you can achieve same. You can add rule to set view at the bottom of parent layout.
Thanks
You have to wrap your LinearLayout in a other Layout. I.e. a RelativeLayout, and add this with 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>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="@+id/rl"
android:layout_height="360dip">
<WebView android:id="@+id/webviewHelp" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button android:id="@+id/My_btn"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" android:gravity="center"
android:textSize="8px" android:text="Download this mp3 file"
android:textColor="@color/white" android:layout_width="fill_parent"
android:layout_height="27dip" />
<Button android:id="@+id/My_btn1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" android:text="this is button !"
android:layout_width="0dip" android:layout_height="0dip"
android:visibility="invisible" />
</RelativeLayout>
精彩评论