膨胀视图后匹配父级宽度

Dar*_*ius 1 android xamarin.android

我的应用程序中有 2 个EditText视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@color/white">
<MyProject.MyView
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="60dp" />
<EditText
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        style="@style/my_style"
        android:hint="my hint2"/>
Run Code Online (Sandbox Code Playgroud)

这是代码MyProject.MyView

public class MyView : LinearLayout
{
    // not important code...

    public LinearLayout Panel { get; private set; }

    // Gets called from the constructor:
    private void Init()
    {
        var layoutInflater = (LayoutInflater)this.Context.GetSystemService(Android.Content.Context.LayoutInflaterService);

        this.content = layoutInflater.Inflate(Resource.Layout.my_content, this.Panel, true);

        this.AddView(this.content);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是my_content

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
style="@style/my_style2">
<EditText
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/my_style"
        android:hint="my hint">

    </EditText>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

第一个EditText(从我的班级中夸大MyView)将与父级的宽度不匹配,它只会包装内容。但第二个EditText与父级的宽度相匹配。为什么?

Dar*_*ius 7

我通过在xml 中用<LinearLayout>a包装解决了这个问题。我不确定为什么需要这样做,所以我仍然希望得到解释,以便我可以更好地理解 Android 的工作方式。<RelativeLayout>my_content