ActionBarView只能用于android:layout_height =" match_parent"

时间:2015-03-03 03:01:42

标签: android android-actionbar

我想设计一个带截图功能的应用。 这是我的班级:

public class ScreenShot {

private static Bitmap takeScreenShot(Activity activity) {

    View view = activity.getWindow().getDecorView();
    view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());  
    // view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap b1 = view.getDrawingCache();

    if (null == b1 || b1.getHeight() == 0 || b1.getWidth() == 0) {
        System.out.println("b1 is null");
    } else {
        System.out.println("b1 is NOT null");
    }

    view.destroyDrawingCache();
    return b1;
}


private static void savePic(Bitmap b, String strFileName) {
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(strFileName);
        if (null != fos) {
            b.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.flush();
            fos.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}


public static void shoot(Activity a) {  
    File sdcardDir = null;
    boolean isSDExist = Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED);
    if (isSDExist) {
        sdcardDir = Environment.getExternalStorageDirectory();
    } else {
        sdcardDir = null;
    }
    String file = sdcardDir + "/xx.png";
    System.out.println(file);
    ScreenShot.savePic(ScreenShot.takeScreenShot(a), file);
    }
}

但它有一些错误:java.lang.IllegalStateException: ActionBarView can only be used with android:layout_height="match_parent"; 我不知道为什么会这样,我没有使用ActionBarView。我的布局只有一个textview和一个relativelayout。

错误日志:03-03 10:56:09.530: E/AndroidRuntime(20452): at android.view.View.measure(View.java:12912);可能view.measure()我使用了哪些错误?

1 个答案:

答案 0 :(得分:0)

  

`View view = activity.getWindow()。getDecorView();   这将检索包含actionbar和actionbar的顶级窗口装饰视图,仅用于height = match_parent

如果您不想使用ActionBar,您必须使用NoActionBar主题(例如Theme.Holo.Light.NoActionBar)

或者将下面代码中的值0更改为-1

  

view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));