So I have created an ImageView
programmatically in my Activity
.
I want to add this ImageView
at the bottom of one of my RelativeLayout
.
This is what I tried:
// creating new ImageView
ImageView shadowView = new ImageView (contextPara);
shadowView.SetBackgroundResource (Resource.Drawable.bottomBar_Shadow);
shadowView.SetScaleType (ImageView.ScaleType.FitXy);
// creating ImageView LayoutParams
WindowManagerLayoutParams imgViewLayoutParams = new WindowManagerLayoutParams ();
imgViewLayoutParams.Width = ViewGroup.LayoutParams.MatchParent;
imgViewLayoutParams.Height = (int)imgViewHeight;
imgViewLayoutParams.Gravity = GravityFlags.Bottom;
// Adding ImageView to RelativeLayout
listViewRelativeLayout.AddView (shadowView, imgViewLayoutParams);
My problem is this adds the ImageView
to the TOP of the RelativeLayout
, NOT the bottom.
I also tried the following:
After I call the AddView
and add the ImageView
, I set the Y
position of the ImageView
.
This actually works. It moves the ImageView
down but I'm not sure how much to move down. RelativeLayout
size is not absolute.
// this moves the ImageView down 100px.
shadowView.SetY (100f);
How can I put it to the bottom of the RelativeLayout
?
Please note: I CAN'T just do the math ( RelativeLayout
height - ImageView
Height) because RelativeLayout
height is always 0
until OnCreate
finishes.
What are my other choices?
Thank you for your time.
Possible Solutions:
1.
Consider placing a single ImageView within the RelativeLayout XML.
You can access this ImageView inside the Activity/Fragment using
ImageView mImageView = (ImageView) view.findViewById(R.id.imageView);
You can set the starting visibility as gone mImageView.setVisibility(View.GONE)
, and show the ImageView at a later time mImageView.setVisibility(View.VISIBLE)
;
2.
You can add a sub-layout (LinearLayout) to the existing RelativeLayout, this sub-layout can be placed at the bottom of the RelativeLayout via XML.
Inside the Activity/Fragment, you can target that sub-layout (linearLayout) by its ID, and than add the ImageView programmatically to that layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.modup.fragment.FeedFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout>
android:id="@+id/linearLayout"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</LinearLayout>
</RelativeLayout>
I found a way!
Instead of using WindowManagerLayoutParams
to set LayoutParameters, using RelativeLayout.LayoutParams
does the job.
Here's how:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MatchParent, (int)imgViewHeight);
layoutParams.AddRule (LayoutRules.AlignParentBottom);
AlignParentBottom
does the job!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.