我可以创建一个独立的;“用户控制”;在Android中包含UI和逻辑/代码?

我可以创建一个独立的;“用户控制”;在Android中包含UI和逻辑/代码?,android,user-controls,components,Android,User Controls,Components,UserControls-我如何在Android中实现它 我在这里提出的一些问题与我在Android中创建“用户控件”的努力有关,即一个可重用的组件,它定义了布局和逻辑/代码,可以放在我需要的任何地方 我希望能够: 能够实例化类,这样我就有了一个实际的对象,我可以访问它的属性、方法等 将UserControl放置在布局xml文件中定义的另一个布局中 在对话框/警报对话框中使用UserControl 例如,我创建了一个MyUserControl的Instance。有了它,我可以将它放在弹出窗口

UserControls-我如何在Android中实现它

我在这里提出的一些问题与我在Android中创建“用户控件”的努力有关,即一个可重用的组件,它定义了布局和逻辑/代码,可以放在我需要的任何地方

我希望能够:

  • 能够实例化类,这样我就有了一个实际的对象,我可以访问它的属性、方法等
  • 将UserControl放置在布局xml文件中定义的另一个布局中
  • 在对话框/警报对话框中使用UserControl
例如,我创建了一个MyUserControl的Instance。有了它,我可以将它放在弹出窗口中并显示,或者在对话框/警报对话框中使用它,或者在某个布局中使用它。在所有情况下,我都可以访问对象(myUserControl),这样我就可以做一些事情(myUserControl.SetObject(MyObject o)或其他事情)

简言之:.NET有一个我正在寻找的概念,它的模块化“UserControl”是一个独立的组件,它本身包含布局和代码(当然,布局XML是一个单独的文件,但没关系)。这有很好的理由,因为我真的很讨厌“代码重复”。我想要一段代码/组件,可以在任何地方使用


这可行吗?

1-为组件创建新的xml文件。示例:MyUserControl.xml -在此文件中,根据需要制作组件。示例:LinearYout有两个按钮

2-为组件创建新类,并从xml文件的根视图扩展它。示例:从LinearLayout扩展

3-将此构造函数添加到类中

 public ClassName(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.yourXmlFile, this);

    }
    public ClassName(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.yourXmlFile, this);
    }

我知道这是一个老问题,但这可能有助于任何人寻找这个一般问题的XAMARIN解决方案/答案-

这篇文章以.net的方式展示了代码隐藏和AXML的分离,以及在代码中使用它的各种方式。下面是一个片段,它使用代码隐藏、AXML作为实际的用户控件,以及如何将用户控件包含在父级AXML中

“UserControl”的XAML代码-


代码隐藏以将此布局加载到控件中并对其进行操作-

namespace CompoundCustomControlView
{
    public class CompoundAXMLView : LinearLayout
    {

    // ...

    void Initialize ()
    {
        SetBackgroundColor (new Android.Graphics.Color (100, 100, 100));

        Inflate (Context, Resource.Layout.CompoundAXMLViewLayout, this);

        var b1 = FindViewById<Button> (Resource.Id.compoundaxmlview_button);
        var tv1 = FindViewById<TextView> (Resource.Id.compoundaxmlview_textview);

        tv1.Text = "Text2";
        b1.Text = "Button2";

        var layout = FindViewById<LinearLayout> (Resource.Id.compoundaxmlview_outerlayout);

        TextView tv = new TextView (Context);
        tv.Text = "Text2.1";
        Button b = new Button (Context);
        b.Text = "Button2.1";

        // Add inside our layout
        layout.AddView (tv);
        layout.AddView (b);
    }
}
名称空间复合CustomControlView
{
公共类CompoundAXMLView:LinearLayout
{
// ...
无效初始化()
{
SetBackgroundColor(新的Android.Graphics.Color(100100100));
膨胀(Context,Resource.Layout.CompoundAXMLViewLayout,this);
var b1=findviewbyd(Resource.Id.compoundaxmlview_按钮);
var tv1=findviewbyd(Resource.Id.compoundaxmlview\u textview);
tv1.Text=“Text2”;
b1.Text=“Button2”;
var布局=FindViewById(Resource.Id.compoundaxmlview\u outerlayout);
TextView tv=新的TextView(上下文);
tv.Text=“Text2.1”;
按钮b=新按钮(上下文);
b、 Text=“按钮2.1”;
//在我们的布局中添加
layout.AddView(电视);
layout.AddView(b);
}
}
}

和代码将其包含在父视图中-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <compoundcustomcontrolview.CompoundAXMLView
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:id="@+id/compound2" />
</LinearLayout>


这是大量的文本。做一个简短的谷歌搜索可能会让你找到一个解释你所问的一切的网站。不,我已经通读了,我已经在谷歌上搜索了几个小时,我已经一遍又一遍地阅读了这个论坛,我还没有找到解决问题的方法。你发布的链接没有任何帮助,因为它根本无法回答我的问题。我建议你尝试使用该页面上几乎没有描述的技术来完成我在帖子中提到的事情。在这件事上没有任何信息可以帮助你。我有trid自定义组件(扩展不同的布局)、扩展对话框等,但没有一个真正符合我的要求。我使用文档中描述的内容构建了您在这里描述的组件,所以我已经尝试过了。您可能误解了内容或没有正确阅读网站,但我相信这正是您所需要的。您能否创建一(1)个“组件”(扩展布局),我可以在对话框、设计时布局中使用,同时能够实例化?我很想知道如何。。。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <compoundcustomcontrolview.CompoundAXMLView
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:id="@+id/compound2" />
</LinearLayout>