我正在开发一个OpenSource lib作为内置android PopupMenu的替代品,在我的lib中我想让所有东西都可以自定义,如果你想使用它并改变弹出颜色/尺寸变得容易。
完整的源代码在这里: https://github.com/shehabic/Droppy/tree/styleable_ui
我有几个问题..简要地说,我了解到要使您的窗口小部件/自定义视图更具风格,您必须为这样的可设置属性定义1-Custom View,2-Styleable属性,3-Default值。 所以我有以下xml文件:
RES / droppy__attr.xml
<resources>
<declare-styleable name="Droppy">
<attr name="droppyMenuItemTitleStyle" format="reference"/>
</declare-styleable>
<declare-styleable name="DroppyMenuItemTitle">
<attr name="android:textColor"/>
<attr name="android:minWidth"/>
<attr name="android:minHeight"/>
<attr name="android:layout_height"/>
<attr name="android:layout_width"/>
<attr name="android:layout_gravity"/>
<attr name="android:gravity"/>
<attr name="android:layout_weight"/>
</declare-styleable>
</resource>
RES / droppy__styles.xml
<resources>
<style name="Theme.DroppyDefaults" parent="android:Theme">
<item name="droppyMenuItemTitleStyle">@style/Droppy.DroppyMenuItemTitle</item>
</style>
<style name="Droppy">
</style>
<style name="Droppy.DroppyMenuItemTitle">
<item name="android:textColor">@color/darkgrey</item>
<item name="android:minWidth">128dp</item>
<item name="android:minHeight">30dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_gravity">end|center_vertical</item>
<item name="android:gravity">center_vertical</item>
<item name="android:layout_weight">1</item>
</style>
</resource>
RES / droppy__defaults.xml
<resources>
<!-- Menu Item Title -->
<color name="default_menu_item_title_textColor">@color/darkgrey</color>
<dimen name="default_menu_item_title_minWidth">128dp</dimen>
<dimen name="default_menu_item_title_minHeight">30dp</dimen>
<dimen name="default_menu_item_title_layout_width">0dp</dimen>
<!-- There's no easy way to set default values for the following -->
<!--<float name="default_menu_item_title_layout_weight">1</float>-->
<!--<item name="default_menu_item_title_layout_gravity">end|center_vertical</item>-->
<!--<item name="default_menu_item_title_gravity">center_vertical</item>-->
</resources>
在上面的区块中,第1期是 现在,这是我在自定义视图中使用的代码:
DroppyMenuItem.java
public class DroppyMenuItemTitle extends TextView {
public DroppyMenuItemTitle(Context context) {
this(context, null);
}
public DroppyMenuItemTitle(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.droppyMenuItemTitleStyle);
}
public DroppyMenuItemTitle(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
final int defaultWidth = ViewGroup.LayoutParams.MATCH_PARENT;
final float defaultWeight = 1;
final int defaultGravity = Gravity.CENTER_VERTICAL;
final int defaultLayoutGravity = Gravity.END | Gravity.CENTER_VERTICAL;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DroppyMenuItemTitle, defStyleAttr, 0);
int width = (int) a.getDimension(R.styleable.DroppyMenuItemTitle_android_layout_width, defaultWidth);
int height = a.getInt(R.styleable.DroppyMenuItemTitle_android_layout_height, ViewGroup.LayoutParams.WRAP_CONTENT);
setGravity(a.getInt(R.styleable.DroppyMenuItemTitle_android_gravity, defaultGravity));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height);
lp.width = width;
lp.height = height;
lp.weight = a.getFloat(R.styleable.DroppyMenuItemTitle_android_layout_weight, defaultWeight);
lp.gravity = a.getInteger(R.styleable.DroppyMenuItemTitle_android_layout_gravity, defaultLayoutGravity);
setLayoutParams(lp);
}
按照以下内容:
int width = (int) a.getDimension(R.styleable.DroppyMenuItemTitle_android_layout_width, defaultWidth);
如果我使用该customView并将width指定为wrap_content或match_parent,则它可以正常工作,因为它们被转换为-1和-2。 但如果将其指定为XXdp(例如20dp)则会失败,因为现在的数字实际上是浮点数不是int
我得到的错误如下:
E / AndroidRuntime(2940):java.lang.NumberFormatException:无效的int:&#34; 20.0dip&#34;