gpt4 book ai didi

android - 在 Android 的 xml 布局中引用来自类的静态字段

转载 作者:太空狗 更新时间:2023-10-29 13:25:25 25 4
gpt4 key购买 nike

假设我在布局中有以下 TextView:

<TextView
android:id="@+id/txtLoginError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/sz_12dp"
android:gravity="center"
android:text="@string/foo" />

是否可以更改 @dimen/sz_12dp 以指向自定义类中的静态字段,例如:

public static class MyDimensions {
public static int topMarginInPixels = 99; // which would be referenced in some fashion like android:layout_marginTop="@class/MyDimensions.topMarginInPixels"
}

我基本上是在寻找一种“数据绑定(bind)”布局元素的方法来进行一些实验;而不是加载 View 然后在代码中修改它(findviewsetWeight 等)我希望在创建期间查看从我的自定义类中获取大小就像它从中获取它一样R.java.

注意:我知道支持多屏幕的最佳实践,px 和 dp 之间的差异,如何为不同的屏幕尺寸提供不同的 res/values 目录,所以请仅在您知道此问题的答案时回答而不是重复 can be read on this link

最佳答案

不可能实现你所描述的,但如果你真的想要像素并且只有像素,你可以很好地放置 px 大小而不是 dp,两者都在XML 或 dimens 文件 - 但我猜你已经知道了。

EDIT 维度确实不能在运行时改变,但它甚至不应该改变;如果您想在运行时更改 View 的尺寸,请获取其 LayoutParams 对象并设置其宽度、高度、边距、填充或您想要更改的任何内容。

关于android - 在 Android 的 xml 布局中引用来自类的静态字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21790597/

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