关闭

自定义activity窗口大小

标签: android开发自定义窗口大小
370人阅读 评论(0) 收藏 举报
分类:

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

<?xml version="1.0" encoding="utf-8"?>
<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文件中

    <style name="Theme.Test" parent="android:style/Theme.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
        <item name="android:height">100dp</item>
        <item name="android:width">100dp</item>
        <item name="android:windowBackground">@drawable/shape_trans_rectangle</item>
    </style>

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);
    }

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17168次
    • 积分:337
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:0篇
    • 译文:0篇
    • 评论:6条