覆盖 xml <include .../>

wts*_*g02 1 xml android android-layout

是否可以在标签中覆盖 xml 中的属性?

父文件

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <include  
        layout="@layout/button"
         android:text="HAHAHAH"
         />
</TableLayout>
Run Code Online (Sandbox Code Playgroud)

按钮.xml:

    <?xml version="1.0" encoding="utf-8"?>
   <TextView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="some string" />
Run Code Online (Sandbox Code Playgroud)

是否可以将“某个字符串”更改为“HAHAHA”?

Mar*_*ski 5

是和否 :) “是”,因为您通常可以覆盖 XML 中的属性,“否”是因为在这种情况下,只有更改android:layout_*属性才会生效:

您还可以android:layout_* 通过在标记中指定它们来覆盖所包含布局的根视图的所有布局参数(任何属性)。例如:

<include android:id=”@+id/news_title”
         android:layout_width=”match_parent”
         android:layout_height=”match_parent”
         layout=”@layout/title”/>
Run Code Online (Sandbox Code Playgroud)

但是,如果要使用标签覆盖布局属性,则必须覆盖 android:layout_height 和 android:layout_width 以使其他布局属性生效。

http://developer.android.com/training/improving-layouts/reusing-layouts.html#Include