在@id的位置使用xml属性中的值文件中的变量

时间:2021-03-02 20:17:15

Is it possible to replace the @id/blah in a View (for example on layout_below) with a variable in a file, and then have two different files, one in each flavor that sets the @id according to the flavor built?

是否可以用文件中的变量替换视图中的@ id / blah(例如在layout_below上),然后有两个不同的文件,每个文件中有一个根据构建的风格设置@id?

e.g.

<TextView
    android:id="@+id/badge"
    style="@style/badge_border"
    android:layout_below="@variable/test_name"
    tools:text="Exclusive"/>

then values/variables.xml

<variable name="test_name" value="@id/view1"/>

1 个解决方案

#1


0  

I don't think it is possible, but why do you need to do that anyway? You can check for the flavor in your code and do different things as needed instead of changing the id based on the flavor. Or you can insert flavor-specific layout differences, such as different id (if you really want to change the id) to a common layout like:

我不认为这是可能的,但为什么你还需要这样做呢?您可以检查代码中的风格并根据需要执行不同的操作,而不是根据风格更改ID。或者,您可以插入特定于特定于风格的布局差异,例如不同的ID(如果您确实要更改ID)到公共布局,例如:

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

And then in res directory of each flavor have the same include file name but with different content like:

然后在每个风味的res目录中都有相同的包含文件名但具有不同的内容,如:

flavor1/res/layout/include_part_of_layout.xml

....
<TextView
android:id="@+id/badge_for_flavor1"
style="@style/badge_border"
android:layout_below="@variable/test_name"
tools:text="Exclusive"/>
...

flavor2/res/layout/include_part_of_layout.xml

....
<TextView
android:id="@+id/badge_for_flavor2"
style="@style/badge_border"
android:layout_below="@variable/test_name"
tools:text="Exclusive"/>
...

#1


0  

I don't think it is possible, but why do you need to do that anyway? You can check for the flavor in your code and do different things as needed instead of changing the id based on the flavor. Or you can insert flavor-specific layout differences, such as different id (if you really want to change the id) to a common layout like:

我不认为这是可能的,但为什么你还需要这样做呢?您可以检查代码中的风格并根据需要执行不同的操作,而不是根据风格更改ID。或者,您可以插入特定于特定于风格的布局差异,例如不同的ID(如果您确实要更改ID)到公共布局,例如:

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

And then in res directory of each flavor have the same include file name but with different content like:

然后在每个风味的res目录中都有相同的包含文件名但具有不同的内容,如:

flavor1/res/layout/include_part_of_layout.xml

....
<TextView
android:id="@+id/badge_for_flavor1"
style="@style/badge_border"
android:layout_below="@variable/test_name"
tools:text="Exclusive"/>
...

flavor2/res/layout/include_part_of_layout.xml

....
<TextView
android:id="@+id/badge_for_flavor2"
style="@style/badge_border"
android:layout_below="@variable/test_name"
tools:text="Exclusive"/>
...