RelativeLayout不尊重父界限

时间:2017-06-05 13:31:16

标签: android android-layout xamarin xamarin.android

我在运行时在 Canvas 中渲染 RelativeLayout 时遇到问题。我知道这是Xamarin.Android,但它实际上与“原创”Android完全相同,所以我会接受所有有效的解决方案。

我有这个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>

这是正确的结果:

Expected result

如果我尝试通过 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不在视图中心):

Actual result

如果你需要“ 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);

1 个答案:

答案 0 :(得分:1)

您需要使用public function store(Request $request) { dd($request->all()); $this->validate($request, array( 'menu_name'=>'required', 'menu_price'=>'required', )); $upcoming = new Upcomingfood; $upcoming->menu_name=$request->menu_name; $upcoming->menu_price=$request->menu_price; $upcoming->save(); Session::flash('success','Food Menu Added Successfullly'); return redirect()->back(); } 的结果作为调用View.MeasureSpec.MakeMeasureSpec的X / Y测量值的来源,然后使用View.Measure,在您的情况下应为50,在View.MeasuredWidth电话中。现在,View及其所有子项都已正确布局,您View.Layout可以Draw

以下是我用来生成一系列倒计时位图的例程的简化版本(它们作为纹理提供给OpenGL)。

Canvas

下图中的顶部ProgressBar在基于包含的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,显示了完全离屏渲染的BitMap相同的RelativeLayout充气axml:

enter image description here