我在运行时在 Canvas 中渲染 RelativeLayout 时遇到问题。我知道这是 Xamarin.Android,但它实际上与“原始”Android 完全相同,所以我会接受所有可行的解决方案。
我有这个在我的 View 中正确呈现的 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);
明确一点,“Native”是我的 Canvas 对象,我正在为 LinearLayout 设置红色背景色,这样我就可以看到它的加载位置我的看法。
我得到的结果很奇怪,LinearLayout 被正确渲染但“score_progress_bar”对象不是,它似乎在某处有边距未设置边界(注意编号为“25”的 TextView 不在 View 中心):
如果您需要“score_progress_xml”,就在这里(它只是一个带有一些 View 的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
的结果作为调用 View.Measure
的 X/Y 测量的原点,然后使用 View.MeasuredWidth
,在您的情况下,在您的 View.Layout
调用中应为 50。现在 View 及其所有子项都已正确布局,您可以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);
下图中顶部的 ProgressBar 在基于 include 的 RelativeLayout
屏幕上呈现,底部的是一个 ImageView
显示一个完整的位图同一个 RelativeLayout 膨胀的 axml 的离屏渲染:
关于android - RelativeLayout 不尊重父边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44370158/