gpt4 book ai didi

android - 间接引用(别名)到android中的自定义xml属性

转载 作者:行者123 更新时间:2023-12-05 06:45:04 30 4
gpt4 key购买 nike

我有一个自定义的安卓 View 。它有一个自定义属性:

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

这种方法在android中可行吗?

最佳答案

似乎(根据 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" />

关于android - 间接引用(别名)到android中的自定义xml属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26402417/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com