动态应用图像样式时出错,为什么?
attrs.xml:
<declare-styleable name="AppTheme">
<attr name="actionbarCompatLogoStyle" format="reference" />
</declare-styleable>
styles.xml:
<style name="Theme.MyApp" parent="android:style/Theme.Light">
<item name="actionbarCompatLogoStyle">@style/ActionBarCompatLogo</item>
</style>
<style name="ActionBarCompatLogo">
<item name="android:layout_width">30dp</item><!-- original image is huge -->
<item name="android:layout_height">30dp</item>
<item name="android:src">@drawable/app_logo</item>
</style>
问题:如果我使用它,图像尺寸将不起作用(大图像):
ImageButton logo = new ImageButton(context, null, R.attr.actionbarCompatLogoStyle);
如果我使用它,它可以工作(小图像,这就是我想要的) :
<ImageView style="@style/ActionBarCompatLogo"></ImageView>
为什么?
attrs.xml:
<declare-styleable name="AppTheme">
<attr name="actionbarCompatLogoStyle" format="reference" />
</declare-styleable>
styles.xml:
<style name="Theme.MyApp" parent="android:style/Theme.Light">
<item name="actionbarCompatLogoStyle">@style/ActionBarCompatLogo</item>
</style>
<style name="ActionBarCompatLogo">
<item name="android:layout_width">30dp</item><!-- original image is huge -->
<item name="android:layout_height">30dp</item>
<item name="android:src">@drawable/app_logo</item>
</style>
Problem: if I use this, image dimensions won't work (huge image):
ImageButton logo = new ImageButton(context, null, R.attr.actionbarCompatLogoStyle);
If I use this, it works (tiny image, which is what I want):
<ImageView style="@style/ActionBarCompatLogo"></ImageView>
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任何以
layout_
为前缀的属性都是 < 的一部分code>LayoutParams 对象。 LayoutParams 是父视图的特殊参数,关于它应该如何布局子视图。您在视图上设置的 LayoutParams 类型取决于您将其添加到的 ViewGroup 类型。每个容器视图类型可以不同,LayoutParams 也可以不同。layout_weight
特定于LinearLayout,layout_below
特定于RelativeLayout等。layout_width
和layout_height
是基础ViewGroup的一部分布局参数。由此得出的结论是 LayoutParams 不是由视图的构造函数解析的,而是由上面的代码未执行的另一个步骤解析的。 (LayoutInflater 涉及父 ViewGroup 的
generateLayoutParams
方法。)由于 LayoutParams 依赖于 View 的预期父级,因此不建议将 LayoutParams 放入样式中。它主要在您从布局 XML 扩充视图时起作用,但它具有与您在此处找到的边缘情况类似的其他含义,并且需要您了解它们。例如,样式可以为 LinearLayout 指定layout_weight,但如果将具有该样式的视图添加到RelativeLayout,则它将不会按预期运行,因为RelativeLayout 不支持layout_weight 。
Any attribute prefixed with
layout_
is part of aLayoutParams
object. LayoutParams are special arguments to the parent view about how it should lay out the child view. The type of LayoutParams you set on a view is dependent on what type of ViewGroup you are adding it to. Each container view type can be different and so can the LayoutParams.layout_weight
is specific to LinearLayout,layout_below
is for RelativeLayout, etc.layout_width
andlayout_height
are part of the base ViewGroup LayoutParams.The takeaway from this is that LayoutParams are not parsed by the view's constructor, they're parsed by another step that your code above isn't doing. (The LayoutInflater involves the parent ViewGroup's
generateLayoutParams
method.)Since LayoutParams are dependent on the intended parent of the View it's not recommended to put LayoutParams in styles. It mostly works when you are inflating views from layout XML but it has other implications similar to the edge case you've found here and requires you to be aware of them. For example, a style may specify
layout_weight
for a LinearLayout but if a view with that style is added to a RelativeLayout instead it will not behave as expected since RelativeLayout does not supportlayout_weight
.据我所知,不可能以编程方式将样式应用到特定视图,只能通过 XML。
您可以做的是在
onCreate()
方法中为您的活动设置主题。请教这个问题: android 在运行时动态更改样式As far as I know it's not possible to apply styles to specific views programmatically, only via XML.
What you can do is to set a theme on your activity inside the
onCreate()
method. Consult this question: android dynamically change style at runtime