在安卓中间接引用(别名)自定义 XML 属性

3
我有一个自定义的Android视图。它有一个自定义属性:
    <com.my.CustomView
        custom:attribute="value_1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

"custom:attribute" 是一个标志,由以下定义:
    <resources>
        <attr name="attribute">
            <flag name="value_1" value="8"/>
            <flag name="value_2" value="9"/>
        </attr>

        <declare-styleable name="CustomView">
            <attr name="lobby_orientation"/>
        </declare-styleable>

    </resources>

我想做的是根据配置修改这个值。例如,它在纵向时应该是value_1,在横向时应该是value_2。为此,我会在values-port中创建一个文件,在values-land中创建另一个文件:
    <resources>
        <what_to_write name="here">value_1</what_to_write>
    </resources>

因此,布局可以是这样的:
    <com.my.CustomView
        custom:attribute="@what_to_write/here"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

这种方法在安卓上可行吗?
1个回答

1
根据http://developer.android.com/guide/topics/resources/providing-resources.html页面,只有简单的值(布局和可绘制项)可以有别名。我发现的方法是将该值放入样式中,而样式文件可以按配置定义。
    <resources>
        <style name="CustomViewStyle">
            <item name="what_to_write">value_1</item>
        </style>
    </resources>

    <com.my.CustomView
        style="CustomViewStyle"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接