如何在xamarin中的另一个axml布局中重用Android axml布局设计

时间:2017-06-03 14:37:27

标签: android-layout xamarin.android

我有一个axml布局设计,我想在另一个下一个布局中使用?如果有可能,我怎么能这样做呢。 我的可重用样本axml代码如下



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:clickable="true"
    android:weightSum="100"
    android:background="@android:color/background_light"
    android:orientation="horizontal">
     <LinearLayout
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="20"
      android:gravity="center_horizontal">
    <refractored.controls.CircleImageView 
     xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/door_image"
        android:layout_width="70dp"
        android:layout_height="70dp"
        app:civ_border_width="0dp"
        app:civ_border_color="#FF000000"
        android:src="@drawable/fingerprint_icon" />
     </LinearLayout>
     </LinearLayout>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

如果您已经知道要重复使用的布局,请创建一个新的XML文件并定义布局。例如,这是一个布局,用于定义要包含在每个活动中的标题栏(titlebar.xml):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/titlebar_bg"
    tools:showIn="@layout/activity_main" >

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@drawable/gafricalogo" />
</FrameLayout>

根视图应该是您希望在添加此布局的每个布局中显示的方式。

使用包含标记

在要添加可重复使用组件的布局中,添加标记。例如,这是一个包含上面标题栏的布局:

<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="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/titlebar"/>

    <TextView android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

    ...

</LinearLayout>

来源:https://developer.android.com/training/improving-layouts/reusing-layouts.html#Include

相关问题