2

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

父.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>

按钮.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" />

是否可以将“some string”更改为“HAHAHA”?

4

1 回答 1

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”/>

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

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

于 2013-01-02T19:15:28.190 回答