Android:Jelly Bean中的setIndicatorBounds或更高版本

时间:2014-05-09 09:24:54

标签: android api expandablelistview

我创建了一个custom ExpandableListView的应用。它工作正常。但我已经知道在Jelly Bean中setIndicatorBounds不能正常工作。我合作:

  • minSdkVersion:14
  • targetSdkVersion:18

我试过了,但它给了我一个错误:

调用需要API级别18(当前最小值为14):android.widget.ExpandableListView #setIndicatorBoundsRelative

    DisplayMetrics metrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int anchoPantalla = metrics.widthPixels;

    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {

        expListView.setIndicatorBounds(anchoPantalla - GetDipsFromPixel(40), 
                anchoPantalla - GetDipsFromPixel(10));

    } else {
        expListView.setIndicatorBoundsRelative(anchoPantalla - GetDipsFromPixel(40), 
                anchoPantalla - GetDipsFromPixel(10));
    }

如何修复我的错误,我的代码可以正常使用Jelly Bean?感谢

1 个答案:

答案 0 :(得分:1)

...
      DisplayMetrics metrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int anchoPantalla = metrics.widthPixels;

    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {

        expListView.setIndicatorBounds(anchoPantalla - GetDipsFromPixel(40), 
                anchoPantalla - GetDipsFromPixel(10));

    } else {
        JB_setIndicatorBoundsRelative(anchoPantalla - GetDipsFromPixel(40), 
                anchoPantalla - GetDipsFromPixel(10));
    }
}    
    ...

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void JB_setIndicatorBoundsRelative(int left, int right)
    {
        expListView.setIndicatorBoundsRelative(left, right);
    }