访问自定义组件的 AttributeSet 中的 attr
我有一个自定义组件,其中包含两个 TextView,它们启用了自定义大小设置方法(两个文本视图的比例约为 1:2)。由于这是RelativeLayout的子类,它没有textSize属性,但我想知道是否仍然可以在该组件的XML实例化中设置android:textSize
属性,然后从 AttributeSet 中获取 textSize
属性,以便与构造函数中的自定义 setSize()
方法一起使用。
我已经看到了使用自定义属性执行此操作的技术,但是如果我想获取 Android 词典中已有的属性该怎么办?
I've got a custom component containing two TextViews that have a custom size-setting method on (the two text views are proportional by about a 1:2 ratio). Being that this is a subclass of RelativeLayout, it doesn't have a textSize attribute, but I'm wondering if it is possible to still set the android:textSize
attribute in the XML instantiation for this component, and then grab the textSize
attribute from the AttributeSet to use with my custom setSize()
method in the constructor.
I've seen the technique to do this with custom attributes, but what if I want to grab an attribute that's already in the android lexicon?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的;
让我们假设您的RelativeLayout 声明(在xml 中)使用14sp 定义了textSize:
在您的自定义视图(采用AttributeSet 的构造函数)中,您可以从Android 的命名空间中检索属性,如下所示:
xmlProvidedSize 的值将是像这样的“14.0sp”,也许通过一点字符串编辑,你就可以提取数字。
声明您自己的属性集的另一种选择可能会有点冗长,但也是可能的。
因此,您的自定义视图和 TextView 声明如下:
太棒了...
现在您还需要确保您的自定义视图覆盖采用 AttributeSet 的构造函数,如下所示:
好的,让我们看看 init() 方法现在:
您可能想知道 R.styleable.MyCustomView、R.styleable.MyCustomView_text1Size 和 R.styleable.MyCustomView_text2Size 来自哪里;请允许我详细说明这些内容。
您必须在 attrs.xml 文件(在值目录下)中声明属性名称,以便无论您在何处使用自定义视图,从这些属性收集的值都将传递到您的构造函数中。
因此,让我们看看如何像您所要求的那样声明这些自定义属性:
这是我的整个 attrs.xml
现在您可以在 XML 中设置 TextViews 的大小,但不能在布局中声明命名空间,具体方法如下:
请注意我如何将命名空间声明为“josh”作为第一行在您的 CustomView 的属性集中。
我希望这对乔希有帮助,
yes it is possible;
let's assume your RelativeLayout declaration (in xml) has textSize defined with 14sp:
In the constructor of your custom view (the one that takes in the AttributeSet), you can retrieve the attributes from Android's namespace as such:
The value of the xmlProvidedSize will be something like this "14.0sp" and maybe with little bit of String editing you can just extract the numbers.
Another option to declare your own attribute set would be little lengthy, but it is also possible.
So, you have your custom view and your TextViews declared something like this right:
great...
Now you need to also make sure your custom view overrides the constructor that takes in the AttributeSet like this:
ok, let's see that init() method now:
You're probably wondering where does R.styleable.MyCustomView, R.styleable.MyCustomView_text1Size and R.styleable.MyCustomView_text2Size are coming from; allow me to elaborate on those.
You have to declare the attribute name(s) in your attrs.xml file (under values directory) so that where ever you get to use your custom view, the values gathered from these attributes will be handed in your constructor.
So let's see how you declare the these custom attributes like you've asked:
Here is my whole attrs.xml
Now you can set your TextViews' size in your XML, but NOT without declaring the namespace in your Layout, here is how:
Please pay attention how I declared the namespace to be "josh" as the first line in your CustomView's attribute set.
I hope this helps Josh,
接受的答案有点长。这是一个精简版本,我希望它会很容易理解。要向自定义视图添加
textSize
属性(或使用dp
/sp
的任何属性),请执行以下步骤。1. 创建自定义属性
创建(或添加以下部分)到 attrs.xml。请注意
维度
格式。2. 在 xml 布局中设置属性。
请注意自定义
app
命名空间。3. 在自定义视图中获取属性值
请注意
getDimensionPixelSize
的使用。在您的自定义视图中,只需使用像素即可。如果您需要转换为不同的维度,请参阅此答案。注释
The accepted answer is a little long. Here is a condensed version that I hope will be easy to follow. In order to add a
textSize
attribute (or anything that usesdp
/sp
) to your custom view, do the following steps.1. Create a custom attribute
Create (or add the following section) to attrs.xml. Note the
dimension
format.2. Set the attribute in your xml layout.
Note the custom
app
namespace.3. Get the attribute value in your custom view
Note the use of
getDimensionPixelSize
. Within your custom view just work with pixels. See this answer if you need to convert to a different dimension.Notes