Xamarin.Android膨胀自定义视图

时间:2019-04-12 06:03:53

标签: c# android xamarin.android android-custom-view custom-view

与Xamarin的另一天。

好吧,让我直接了解一下:我对android开发以及Xamarin都是陌生的

因此,我试图在项目中创建自定义View,并尝试从布局中设置(或inflate)它的视图。我已经创建了一个布局,并尝试从View对其进行充气。

CustomLayout.axml

<?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="match_parent"
android:background="#fff">
   <EditText android:layout_height="120dp"
   android:layout_width="120dp"/>
</LinearLayout>

Customlayout.cs

public class Customlayout : LinearLayout
{
    public Customlayout(Context context) : base(context)
    {
        Inin();
    }
    public Customlayout(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        Inin();
    }

    public Customlayout(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
    {
        Inin();
    }
    private void Inin()
    {
        LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
        View view = inflater.Inflate(Resource.Layout.CustomLayout, this, true);
        this.AddView(view);
    }
}

该代码对视图没有任何影响。当我以任何其他布局引用它时,它保持空白。我知道我的代码是不正确的,我只是在经历了无数与Java和android相关的问题后才尝试使用此代码。我什至不确定这是否可行。

关于如何扩大视图的任何想法?

2 个答案:

答案 0 :(得分:2)

像这样对我有效:

Customlayout.cs

像这样更改Init()

private void Init()
  {  
   LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
   View view = inflater.Inflate(Resource.Layout.customlayout, null, true);
   this.AddView(view);
  }

然后在您的活动布局.axml 中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <namespace.CustomLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
   />
</LinearLayout>

答案 1 :(得分:0)

尝试一下

<?xml version="1.0" encoding="utf-8"?>
    <Customlayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:orientation="vertical">

        <EditText
            android:layout_width="120dp"
            android:layout_height="120dp" />
    </Customlayout>

您必须提供正确的软件包名称。它将看起来像这样your_packageName.Customlayout

public class Customlayout : LinearLayout
{
    public Customlayout(Context context) : base(context)
    {
        Inin();
    }
    public Customlayout(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        Inin();
    }

    public Customlayout(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
    {
        Inin();
    }
    private void Inin()
    {

    }
}

像往常一样将CustomLayout.axml加载到FragmentActivity中。