[英]Add ImageView to RelativeLayout in Android
So I have created an ImageView
programmatically in my Activity
. 因此,我在
Activity
以编程方式创建了ImageView
。
I want to add this ImageView
at the bottom of one of my RelativeLayout
. 我想将此
ImageView
添加到我的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. 我的问题是,这会将
ImageView
添加到RelativeLayout
的顶部,而不是底部。
I also tried the following: 我还尝试了以下方法:
After I call the AddView
and add the ImageView
, I set the Y
position of the ImageView
. 我所说的后
AddView
并添加ImageView
,我设置了Y
的位置ImageView
。
This actually works. 这实际上有效。 It moves the
ImageView
down but I'm not sure how much to move down. 它将
ImageView
向下移动,但是我不确定向下移动多少。 RelativeLayout
size is not absolute. RelativeLayout
大小不是绝对的。
// this moves the ImageView down 100px.
shadowView.SetY (100f);
How can I put it to the bottom of the RelativeLayout
? 如何将其放在
RelativeLayout
的底部?
Please note: I CAN'T just do the math ( RelativeLayout
height - ImageView
Height) because RelativeLayout
height is always 0
until OnCreate
finishes. 请注意:我不能只是做数学运算(
RelativeLayout
height- ImageView
Height),因为在OnCreate
完成之前RelativeLayout
高度始终为0
。
What are my other choices? 我还有其他选择吗?
Thank you for your time. 感谢您的时间。
Possible Solutions: 可能的解决方案:
1. 1。
Consider placing a single ImageView within the RelativeLayout XML. 考虑在RelativeLayout XML中放置一个ImageView。
You can access this ImageView inside the Activity/Fragment using 您可以使用以下方式在“活动/片段”中访问此ImageView:
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)
; 您可以设置起始能见度消失
mImageView.setVisibility(View.GONE)
并显示在稍后的时间ImageView的mImageView.setVisibility(View.VISIBLE)
;
2. 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. 您可以在现有的RelativeLayout上添加一个子布局(LinearLayout),该子布局可以通过XML放置在RelativeLayout的底部。
Inside the Activity/Fragment, you can target that sub-layout (linearLayout) by its ID, and than add the ImageView programmatically to that layout. 在“活动/片段”内部,您可以通过其ID定位该子布局(linearLayout),然后以编程方式将ImageView添加到该布局。
<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. 使用
RelativeLayout.LayoutParams
完成此工作,而不是使用WindowManagerLayoutParams
来设置LayoutParameters。
Here's how: 这是如何做:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MatchParent, (int)imgViewHeight);
layoutParams.AddRule (LayoutRules.AlignParentBottom);
AlignParentBottom
does the job! AlignParentBottom
可以完成工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.