AppWidget Screen Orientation

你离开我真会死。 提交于 2019-12-11 18:51:39

问题


Here is my question. I have an appwidget which works perfectly in a portrait screen orientation. However, When i changed my screen orientation to landscape my appWidget's width and height changing automatically by the home screen launcher and the problem is in landscape mode my widget's height is not set properly by the homescreen launcher. So, I need to detect is screen orientation changed and re-size my appwidget. I've googled it but still i don't have any idea how can i do that. If it was an activity it was simple but it's not. I'll be gratefull if anyone has idea about this.


回答1:


When the screen orientation changes your AppWidgetProvider onUpdate method gets called. You can use the following function to get if you are in portrait or landscape mode and then update your remote views based on that.

private static boolean isPortrait (Context cx) {
Display d = ((WindowManager) cx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

if (d.getWidth() == d.getHeight()) {
    return false;
} else if (d.getWidth() < d.getHeight()) {
    return true;    
} else {
    return false;
}
}

Hope this helps.




回答2:


  1. Define bools.xml in both values-port and values-land

    In values-port/bools.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="portrait">true</bool>
    </resources>
    

    In values-land/bools.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="portrait">false</bool>
    </resources>
    
  2. Get boolean by Context.getResources() in code

    private static boolean isPortrait(Context context) {
        return context.getResources().getBoolean(R.bool.portrait);
    }
    


来源:https://stackoverflow.com/questions/11173249/appwidget-screen-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!