【问题标题】:Android - Get highlight color of listview programatically (backward compatibly)Android - 以编程方式获取列表视图的突出显示颜色(向后兼容)
【发布时间】:2013-11-25 10:46:12
【问题描述】:

当用户按下列表视图项目时,它会突出显示,并在松开时返回其原始颜色。 我想以编程方式查询这些是什么颜色。

在 14 之后的 Api 版本上,我似乎可以使用 (i) Theme.resolveAttribute(int resid, TypedValue outValue, boolean resolveRefs) 或 (ii) Theme.obtainStyledAttributes(int[] attrs)

    //-- (i) ---------------------------------------------
    int getAttrVal(int attr)
    {
        TypedValue Val = new TypedValue();
        getContext().getTheme().resolveAttribute(attr, Val, true);
        return Val.data;
    }


    int colBgd = getAttrVal(android.R.attr.colorBackground);
    int colAh = getAttrVal(android.R.attr.colorActivatedHighlight);
     int colPh = getAttrVal(android.R.attr.colorPressedHighlight);  // not sure if this or prev is what im looking for!?

    // (ii) ---------------------------------------------        
     TypedArray Arr = getContext().getTheme().obtainStyledAttributes(
                      new int[] {android.R.attr.colorPressedHighlight});
      int colPh2 = arrTst.getColor(0, 0xFF00FF);
    //--------------------------------------------------

在较旧的 Api 版本上,这将不起作用,因为 android.R.attr.colorActivatedHighlight/colorPressedHighlight 还没有。

(1) 是否可以在 api 8 上以编程方式获取这些颜色? (2) 我对 Theme 中的其他一些功能有点困惑

例如 (iii) Theme.obtainStyledAttributes(int resid, int[] attrs)

    // ---------------------------------------------                     
     TypedArray Arr2 = getContext().getTheme().obtainStyledAttributes(
               android.R.attr.colorBackground, new int[] {android.R.attr.colorPressedHighlight});
                int colTst2 = arrTst.getColor(0, 0xFF00FF);

    Here it seems not to matter what value I use for resid. What's the point of this parameter?     

(3) R.attr 和 R.styleable 有什么区别? R.attr 应该是 R.styleable 的替代品(并且基本上有相应的条目),还是它们的目的只是部分重叠。

【问题讨论】:

    标签: android listview colors


    【解决方案1】:

    您应该使用以下项目声明选择器:

    <item android:drawable="@color/selected" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="@color/selected" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="@color/selected" android:state_focused="true"/>
    <item android:drawable="@color/normal" android:state_focused="false" android:state_pressed="false"/>
    

    并将其设置为 listRow 的背景

    【讨论】:

    • 感谢您的回复...“将其设置为背景”...听起来您误解了我的意思。我的问题不是设置任何颜色,而是查询正在使用的颜色(已经)......“我想以编程方式查询这些颜色是什么。”......你的声明究竟实现了什么?提前致谢
    • 哦是的..对不起,但是你需要知道颜色是为了什么?顺便说一下, stylable 是视图的样式, attr 用于声明视图可以具有的任何属性,并且可以通过 Style(styelable) 修改 :)
    • 抱歉,这根本没有回答我的问题
    猜你喜欢
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多