2

我正在开发一个开源库作为内置 android PopupMenu 的替代品,在我的库中,我想让所有内容都可以自定义,这样如果你想使用它并更改弹出颜色/尺寸,它就会变得很容易。

完整的源代码在这里: https ://github.com/shehabic/Droppy/tree/styleable_ui

我很少有问题.. 简要地我了解到,要使您的小部件/自定义视图可样式化,您必须为此类可样式化属性定义 1-自定义视图、2-可样式属性、3-默认值。所以我有以下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>

在上面的块中,这是第一个问题,现在这是我在自定义视图中使用的代码:

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 并将宽度指定为 wrap_content 或 match_parent,它可以正常工作,因为它们被转换为 -1 和 -2。但是如果将其指定为 XXdp(例如 20dp)它会失败,因为现在的数字实际上是一个浮点数而不是整数

我得到的错误如下:

E/AndroidRuntime(2940): java.lang.NumberFormatException: Invalid int: "20.0dip"

4

0 回答 0