我的应用中有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"/>
这是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);
}
}
这是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>
第一个EditText
(从我的MyView
类中膨胀)与父母的宽度不符,它只会包装内容。但是第二个EditText
与父母的宽度相匹配。为什么?
我通过在我的my_content
XML中用<RelativeLayout>
包装<LinearLayout>
来解决它。我不确定为什么需要这一点,所以我仍然会欣赏一个解释,以便我可以更好地理解Android的工作方式。