将xml导入另一个xml

Bla*_*elt 47 xml android

我在我的xml(Button例如)中重复了很多控制.是否有可能Button在一个a中编写一次xml,然后在我需要的所有布局中导入它?

Lab*_*lan 102

您可以使用

<include  layout="@layout/commonlayout" android:id="@+id/id" />
Run Code Online (Sandbox Code Playgroud)

commonlayout.xml应该在res/layout可以添加重复部分的位置定义.


Tsi*_*moX 23

正如Labeeb P正确地说,它有效.只想添加你也可以覆盖参数:

<include  
        layout="@layout/commonlayout" 
        android:id="@+id/id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="2sp"
 />
Run Code Online (Sandbox Code Playgroud)