簡體   English   中英

Xamarin.Android膨脹自定義視圖

[英]Xamarin.Android Inflate a 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相關的問題后才嘗試使用此代碼。 我什至不確定這是否可行。

關於如何增加視圖的任何想法?

它對我來說是這樣的:

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);
  }

然后在您的活動layout.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>

嘗試這個

<?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()
    {

    }
}

像往常一樣在您的FragmentActivity加載CustomLayout.axml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM