C# Android为自定义视图充气

C# Android为自定义视图充气,c#,android,xamarin.android,android-custom-view,custom-view,C#,Android,Xamarin.android,Android Custom View,Custom View,和Xamarin再过一天 好吧,让我来亲自澄清一下:我对android开发和Xamarin都是新手 所以,我试图在我的项目中创建一个自定义视图,并试图从布局中设置或放大它的视图。我已经创建了一个布局,并试图从视图中对其进行充气 CustomLayout.axml 代码对视图没有任何影响。当我在任何其他布局中引用它时,它将保持空白。我知道我的代码不正确,我只是在经历了大量与Java和android相关的问题后尝试了这段代码。我甚至不确定这是不是一条路 有没有关于如何放大视图的想法?试试这个 <

和Xamarin再过一天

好吧,让我来亲自澄清一下:我对android开发和Xamarin都是新手

所以,我试图在我的项目中创建一个自定义视图,并试图从布局中设置或放大它的视图。我已经创建了一个布局,并试图从视图中对其进行充气

CustomLayout.axml

代码对视图没有任何影响。当我在任何其他布局中引用它时,它将保持空白。我知道我的代码不正确,我只是在经历了大量与Java和android相关的问题后尝试了这段代码。我甚至不确定这是不是一条路

有没有关于如何放大视图的想法?

试试这个

<?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>
像平常一样在片段或活动中加载CustomLayout.axml。

试试这个

<?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>

像平常一样在片段或活动中加载CustomLayout.axml。

它对我的作用如下:

在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);
  }
然后在activity layout.axml中:


它对我来说是这样的:

在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);
  }
然后在activity layout.axml中:



看起来你把事情弄复杂了。你可以在xml中使用Customlayout而不是LinearLayout,还可以去掉Customlayout中的AddView部分。@sunilsunny,你是说我应该继承Customlayout?我如何摆脱AddView?我尝试从CustomLayout继承,但不确定哪个程序集包含它。我应该引用哪一个?我会加上它作为答案,试试看。@sunilsunny,当然!我在等它。它现在能用了吗?看来你把它弄复杂了。你可以在xml中使用Customlayout而不是LinearLayout,并且可以去掉Customlayout中的AddView部分。@sunilsunny,你是说我应该继承Customlayout?我如何摆脱AddView?我尝试从CustomLayout继承,但不确定哪个程序集包含它。我应该引用哪一个?我会加上它作为答案,试试看。@sunilsunny,当然!我在等它,现在行吗?不行。试过了,现在的视图只是一个灰色的背景,中间的类名!你在预览屏幕上看到了,对吗?。尝试在设备上运行。是的,完全正确。这意味着当我在CustomLayout.axml文件中使用mynamespace.CustomLayout时,它会变为空白-这意味着设计器失败。设计器通常会失败,但如果在设备上运行它,它应该可以工作。尝试过了。。。应用程序在模拟器中崩溃,没有任何异常。无法工作。试过了,现在的视图只是一个灰色的背景,中间的类名!你在预览屏幕上看到了,对吗?。尝试在设备上运行。是的,完全正确。这意味着当我在CustomLayout.axml文件中使用mynamespace.CustomLayout时,它会变为空白-这意味着设计器失败。设计器通常会失败,但如果在设备上运行它,它应该可以工作。尝试过了。。。应用程序在模拟器中崩溃,没有任何异常。
private void Init()
  {  
   LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
   View view = inflater.Inflate(Resource.Layout.customlayout, null, true);
   this.AddView(view);
  }
<?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>