自定义activity窗口大小

1.创建一个背景配置文件,放到res/drawable目录下


<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    
    <solid android:color="#9000" />
    
    <stroke
        android:width="1dp"
        android:color="@color/white50" />
    
    <corners android:radius="3dp" />
    
    <padding
        android:bottom="3dp"
        android:left="3dp"
        android:right="3dp"
        android:top="3dp" />

shape>

2.定义一个对话框样式,放到res/values/styles.xml文件中

    

3.修改Manifes.xml,将我们刚才创建的样式应用到activity上面

        <activity
            android:name=".activity.cgo4.TestActivity"
            android:screenOrientation="landscape"
            android:theme="@style/Test" />

不出意外,到此已经大功告成.
但是我使用了v4包里的drawlayout控件,出现了如下异常:

java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:591)
    at android.view.View.measure(View.java:15172)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15172)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
    ....

发现问题是:
drawlayout的height和width设置为match_parent问题.
All I have is a fragment whose root view is a DrawerLayout with width and height match_parent. I get the DrawerLayout must be measured with MeasureSpec.EXACTLY exception at runtime (although it looks fine at design time now).

解决方法:
extending the DrawerLayout, and overriding onMeasure() .

public class CustomDrawerLayout extends DrawerLayout {

    public CustomDrawerLayout(Context context) {
        super(context);
    }

    public CustomDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(
                MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}

你可能感兴趣的:(android开发)