动态加载xml控件并共享UI控件 [英] Dynamically load xml control and share UI control

查看:26
本文介绍了动态加载xml控件并共享UI控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 xamarin 的新手.是否可以在 xml 文件中加载共享 UI 控件,如果是,如何加载?我愿意接受更好的想法.

I am new to xamarin. Is it possible to load a shared UI control in an xml file, if yes, how? I am open to better idea.

下面是通过include的静态加载,其中parent.axml静态加载child.xml.

Below is a static loading via include, where parent.axml statically load child.xml.

 <include
            android:id="@+id/child"
            layout="@layout/child"
            android:layout_below="@id/toolbar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

父.axml

<?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">
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"
         android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <include
        android:id="@+id/child"
        layout="@layout/child"
        android:layout_below="@id/toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/parent_layout"
        android:layout_below="@id/child">
        <TextView
            android:id="@+id/parent_text"
            android:layout_margin="16dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Click to go to the moon!"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:singleLine="true" />
    </LinearLayout>
</LinearLayout>

子文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"> 
    <TextView
        android:id="@+id/child_text"
        android:layout_margin="16dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="You made it!"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>

这个想法是它可以实现UI控件的共享.在这种情况下,parent.axml 根据条件将 child.xml 加载到其占位符中.

The idea is that it can achieve share of UI controls. In this case, parent.axml loads child.xml into its place holder based on condition.

推荐答案

您可以使用 LayoutInflater 来扩充布局并将它们自动附加到父级(或不附加):

You can use LayoutInflater to inflate layouts and auto-attach them to the parent (or not):

var linearLayout = LayoutInflater.Inflate(Resource.Layout.Child, parent, true);
var textView = linearLayout.FindViewById<TextView>(Resource.Id.child_text);
textView.Text = "Inflated and attached to Parent";

另外,根据您的用例,使用自包含的 Fragment 并将其添加到您的父级"可能是更好的解决方案.

Also depending upon your use-case, using a self-contained Fragment and adding that to your "parent" might be better a better solution.

这篇关于动态加载xml控件并共享UI控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆