RelativeLayout不尊重父界限
我在运行时在画布内呈现RelativeLayout时遇到问题。我知道这是Xamarin.Android,但它实际上与“原始”Android非常相似,所以我会接受所有可行的解决方案。RelativeLayout不尊重父界限
我有这样的XML正确呈现我的观点里(向下滚动,为 “score_progress_bar” XML):
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include
layout="@layout/score_progress_bar"
android:layout_width="50dp"
android:layout_height="50dp" />
</LinearLayout>
这里是正确的结果:
如果我尝试通过Canvas加载相同的东西,我没有得到相同的结果。这里就是我试图在运行时加载相同的控制代码:
var id = Application.Context.Resources.GetIdentifier("score_progress_bar", "layout", Application.Context.PackageName);
var ll = new LinearLayout(Application.Context);
ll.SetBackgroundColor(AndroidColor.Red);
ll.Measure(50, 50);
ll.Layout(0, 0, 50, 50);
var li = (LayoutInflater)Application.Context.GetSystemService(Context.LayoutInflaterService);
var v = (RelativeLayout)li.Inflate(id, null, true);
v.LayoutParameters = new RelativeLayout.LayoutParams(50, 50);
v.Measure(50, 50);
v.Layout(0, 0, 50, 50);
ll.AddView(v);
ll.Draw(Native);
只要是明确的,“本地”是我的画布对象,我设置一个红色的背景色为的LinearLayout所以我可以看到这是在我的视图中加载的位置。
我得到的结果是怪异的的LinearLayout是被正确渲染,但在“score_progress_bar”对象不是,现在看来似乎有边距或某个地方的界限没有设置(注意, TextView中以数字 “25” 是不是在观察中心):
如果您需要 “score_progress_xml”,这里是(它只是一个的RelativeLayout有一些看法):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/score_white_background"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp" />
<ProgressBar
android:id="@+id/progressRing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indeterminate="false"
android:progressDrawable="@drawable/score_background_ring"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="25" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="25"
android:textSize="17dp"
android:textColor="#000000"
android:id="@+id/progressText"
android:gravity="center" />
</RelativeLayout>
你对这里发生了什么有什么想法吗?
编辑:发现解决方案感谢SushiHangover! 刚刚完成的情况下,其他人可能需要它这个问题,这是我在这个特定的情况下,如何解决:
var id = Application.Context.Resources.GetIdentifier("score_progress_bar", "layout", Application.Context.PackageName);
var li = (LayoutInflater)Application.Context.GetSystemService(Context.LayoutInflaterService);
var progressView = (RelativeLayout)li.Inflate(id, null, true);
var measuredWidth = View.MeasureSpec.MakeMeasureSpec(50, MeasureSpecMode.Exactly);
var measuredHeight = View.MeasureSpec.MakeMeasureSpec(50, MeasureSpecMode.Exactly);
progressView.Measure(measuredWidth, measuredHeight);
progressView.Layout(0, 0, progressView.MeasuredWidth, progressView.MeasuredWidth);
progressView.Draw(Native);
您需要使用的结果从View.MeasureSpec.MakeMeasureSpec
为你的X/Y测量的产地为呼叫到View.Measure
,然后使用View.MeasuredWidth
,在你的情况下,你的View.Layout
调用应该是50。现在视图及其所有子项都正确布置,您可以将Draw
添加到您的Canvas
。
下面是我用来生成一系列倒计时位图的例程的一个版本(它们被作为纹理馈给OpenGL)。
var xy = 200;
var inflater = (LayoutInflater)ApplicationContext.GetSystemService(LayoutInflaterService);
var progressIncludeID = ApplicationContext.Resources.GetIdentifier("progress_include", "layout", ApplicationContext.PackageName);
var pseudoProgressView = (RelativeLayout)inflater.Inflate(progressIncludeID, null, false);
int measuredWidth = View.MeasureSpec.MakeMeasureSpec(xy, MeasureSpecMode.Exactly);
int measuredHeight = View.MeasureSpec.MakeMeasureSpec(xy, MeasureSpecMode.Exactly);
pseudoProgressView.Measure(measuredWidth, measuredHeight);
pseudoProgressView.Layout(0, 0, pseudoProgressView.MeasuredWidth, pseudoProgressView.MeasuredWidth);
var imageBitmap = Bitmap.CreateBitmap(xy, xy, Bitmap.Config.Argb8888);
var canvas = new Canvas(imageBitmap);
pseudoProgressView.Draw(canvas);
图像下面内呈现在顶进度一个包括基础的屏幕上的RelativeLayout
和底部的一个是ImageView
表示完全离屏描绘的相同的RelativeLayout的位图充气axml :
在我来说,特别是我已经做到了,而无需创建一个新的位图,因为渲染直接应用到画布上,但它的作品。谢谢! – xTuMiOx