在使用TouchWiz启动器的ICS手机上,Widget margins无法应用?[英] Widget margins not applied on ICS phone with TouchWiz launcher?

本文是小编为大家收集整理的关于在使用TouchWiz启动器的ICS手机上,Widget margins无法应用?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个带有小部件的应用程序.我知道Android 4.0或更高版本将自动在窗口小部件周围创建边距,所以我在此开发人员API页面使小部件在不同的Android版本上大致相同.我在API 10,14和15上测试了模拟器中的小部件,它可以正常工作.窗口小部件在所有版本中都看起来也一样.但是,当我使用Android 4.0.3在我的SGS2手机上测试它时,没有边距!怎么会这样?我错过了什么?有没有别人体验这种行为?

仿真器API 15在此输入图像描述

这是我的 res/xml/widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 

    android:initialLayout="@layout/widget"    
    android:minHeight="40dp"  
    android:minWidth="110dp"
    android:updatePeriodMillis="60000" >

</appwidget-provider>

res/layout/widget.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget_mainlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_margin="@dimen/widget_margin"
    android:background="@drawable/widget_background" >             
</RelativeLayout>

res/drawable/widget_background.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

<stroke
    android:width="2dp"
    android:color="#222222" />

<gradient
    android:angle="225"
    android:startColor="#DD2ECCFA"
    android:endColor="#DD000000" />

<corners android:radius="7dp" />

</shape>

res/值/sime.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="widget_margin">8dp</dimen>
</resources>

res/value-v14/sime.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="widget_margin">0dp</dimen>  
</resources>
首先,我认为它与窗口小部件没有正确更新的小部件如上所述 ,但删除小部件并再次添加它没有帮助.我还通过设置尝试了一下:

  • 设置maxHeight和maxWidth在widget_info.xml文件中不起作用,似乎由仿真器和手机忽略.
  • 将layout_margin更改为padding在widget.xml文件中没有效果,窗口小部件看起来相同(但在仿真器API10小部件上,现在也没有边距)
  • 将0dp更改为0dp到res/values-v14/dimens.xml文件中的文件在手机和仿真器中都有效果,但手机和仿真器上的窗口小部件仍然不同.

编辑: 我在手机上安装了Go Launcher EX,然后窗口小部件确实有了边缘,所以我猜这是三星的TouchWiz启动器的特定问题?任何人都可以确认吗?我也对解决方案感兴趣,以便在使用TouchWiz时使小部件看起来(或多或少)相同.

推荐答案

您已经指出,诀窍是为TouchWiz设备或SDK级别使用不同的边距/填充<14.对于SDK级别<14,您应该继续使用summ.xml,但是您将继续使用thewwiz需要创建一个单独的布局文件.然后,如果检测到TouchWiz,则在窗口小部件RemoteView的构造函数中切换布局文件:

RemoteViews views = new RemoteViews(
    context.getPackageName(), 
    isAlternativeMargins(context) ? 
        R.layout.alternative_layout : 
        R.layout.normal_layout
)
要检测接触温度和其他发射器具有相同的问题,可以使用以下代码:

/* sorted alphabetically to be able to use Arrays.binarySearch later on */
private final static String[] LAUNCHER_ALTERNATIVES = {
    "com.sec.android.app.launcher",
    "com.sec.android.app.twlauncher",
};

private final static String[] LAUNCHER_ALTERNATIVE_EXCEPTIONS = {
    "GT-I9100", // Galaxy S II
    "GT-I9100G", // Galaxy S II
    "GT-I9100T", // Galaxy S II
    "GT-I9210T", // Galaxy S II
    "GT-I9100M", // Galaxy S II
    "SGH-I757M", // Galaxy S II
    "SGH-I727R", // Galaxy S II
    "SGH-I927", // Galaxy S II
    "SGH-T989D", // Galaxy S II
    "GT-I9108", // Galaxy S II
    "GT-I9100P", // Galaxy S II
    "ISW11SC", // Galaxy S II
    "SC-02C", // Galaxy S II
    "SHW-M250K", // Galaxy S II
    "SHW-M250L", // Galaxy S II
    "SHW-M250S", // Galaxy S II
    "SGH-I777", // Galaxy S II
    "SGH-I727", // Galaxy S II
    "SGH-I927", // Galaxy S II
    "SPH-D710", // Galaxy S II
    "SGH-T989", // Galaxy S II
    "SCH-R760", // Galaxy S II
    "GT-N7000", // Galaxy Note
    "SHV-E160K", // Galaxy Note
    "SHV-E160L", // Galaxy Note
    "SHV-E160S", // Galaxy Note
    "SGH-I717", // Galaxy Note
    "SC-05D", // Galaxy Note
    "SGH-T879", // Galaxy Note
    "GT-I9220", // Galaxy Note
    "GT-I9228", // Galaxy Note
    "SCH-I889", // Galaxy Note
    "SGH-I717", // Galaxy Note
};

static {
    Arrays.sort(LAUNCHER_ALTERNATIVE_EXCEPTIONS);
}


public static boolean isAlternativeMargins(Context context) {
    if (Build.VERSION.SDK_INT < 14)
        return false;

    PackageManager pm = context.getPackageManager();
    Intent i = new Intent(Intent.ACTION_MAIN);
    i.addCategory(Intent.CATEGORY_HOME);
    i.addCategory(Intent.CATEGORY_DEFAULT);

    ResolveInfo info = pm.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY);

    if (
            info != null && info.activityInfo != null && 
            info.activityInfo.taskAffinity != null
    ) {
        int pos = Arrays.binarySearch(
                LAUNCHER_ALTERNATIVES, info.activityInfo.taskAffinity
        );

        // if TouchWiz
        if (pos >= 0) {
            // if >= Android 4.1.2 and (Galaxy S II or Galaxy Note) 
            if ( getSdk() >= 16 && Arrays.binarySearch(
                    LAUNCHER_ALTERNATIVE_EXCEPTIONS, Build.MODEL
            ) >= 0)
                return false;

            return true;
        }
    }
    return false;
}

上面的代码检测到默认的启动器并返回true,如果它是触摸的.

编辑:修改了其他发射器,似乎与touchwiz具有相同的问题.

编辑2:在Galaxy S II和Galaxy Note上修改Android 4.1.2,其中小部件边距按预期工作.与Android 4.1.2的Galaxy S 3相比,它们不像预期工作,这可能会改变Samsung的未来TouchWiz版本.

本文地址:https://www.itbaoku.cn/post/102613.html

问题描述

I have an app with a widget. I know that Android 4.0 or later will automatically create a margin around the widget so I've implemented the suggestion on this developers API page to make the widgets approximately the same size on different Android versions. I've tested the widget in the emulator on API 10, 14 and 15 and it works fine. The widget looks the same in all versions. However, when I test it on my SGS2 phone with Android 4.0.3 there are no margins! How can this be? What am I missing? Did anyone else experience this behaviour?

Widget on emulator API 15!enter image description here

Here's my res/xml/widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 

    android:initialLayout="@layout/widget"    
    android:minHeight="40dp"  
    android:minWidth="110dp"
    android:updatePeriodMillis="60000" >

</appwidget-provider>

res/layout/widget.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget_mainlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_margin="@dimen/widget_margin"
    android:background="@drawable/widget_background" >             
</RelativeLayout>

res/drawable/widget_background.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

<stroke
    android:width="2dp"
    android:color="#222222" />

<gradient
    android:angle="225"
    android:startColor="#DD2ECCFA"
    android:endColor="#DD000000" />

<corners android:radius="7dp" />

</shape>

res/values/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="widget_margin">8dp</dimen>
</resources>

res/values-v14/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="widget_margin">0dp</dimen>  
</resources>

At first I thought it had something to do with the widget not being properly updated as described here, but removing the widget and adding it again doesn't help. I've also experimented a bit with the settings:

  • Setting maxHeight and maxWidth in the widget_info.xml file doesn't work, it seems to be ignored by both the emulator and the phone.
  • Changing layout_margin to padding in the widget.xml file has no effect, widget looks the same (but on emulator API10 widget now also has no margins)
  • Changing the 0dp to 16dp in the res/values-v14/dimens.xml file does have an effect in both the phone and the emulator, but the widget's size on the phone and emulator still is different.

EDIT: I've installed GO Launcher Ex on the phone and then the widget does have margins, so I guess this is a specific issue for Samsung's TouchWiz launcher? Can anyone confirm this? I am also interested in solutions to make the widget look (more or less) the same when using TouchWiz.

推荐答案

As you already pointed out, the trick is to use different margins/padding for TouchWiz devices or SDK level < 14. For SDK level < 14 you should continue to use dimens.xml, but for TouchWiz you'll need to create a separate layout file. Then if you detect TouchWiz, you switch the layout file in the constructor of the widget RemoteViews:

RemoteViews views = new RemoteViews(
    context.getPackageName(), 
    isAlternativeMargins(context) ? 
        R.layout.alternative_layout : 
        R.layout.normal_layout
)

To detect TouchWiz and other launchers having the same problem, you could use the following code:

/* sorted alphabetically to be able to use Arrays.binarySearch later on */
private final static String[] LAUNCHER_ALTERNATIVES = {
    "com.sec.android.app.launcher",
    "com.sec.android.app.twlauncher",
};

private final static String[] LAUNCHER_ALTERNATIVE_EXCEPTIONS = {
    "GT-I9100", // Galaxy S II
    "GT-I9100G", // Galaxy S II
    "GT-I9100T", // Galaxy S II
    "GT-I9210T", // Galaxy S II
    "GT-I9100M", // Galaxy S II
    "SGH-I757M", // Galaxy S II
    "SGH-I727R", // Galaxy S II
    "SGH-I927", // Galaxy S II
    "SGH-T989D", // Galaxy S II
    "GT-I9108", // Galaxy S II
    "GT-I9100P", // Galaxy S II
    "ISW11SC", // Galaxy S II
    "SC-02C", // Galaxy S II
    "SHW-M250K", // Galaxy S II
    "SHW-M250L", // Galaxy S II
    "SHW-M250S", // Galaxy S II
    "SGH-I777", // Galaxy S II
    "SGH-I727", // Galaxy S II
    "SGH-I927", // Galaxy S II
    "SPH-D710", // Galaxy S II
    "SGH-T989", // Galaxy S II
    "SCH-R760", // Galaxy S II
    "GT-N7000", // Galaxy Note
    "SHV-E160K", // Galaxy Note
    "SHV-E160L", // Galaxy Note
    "SHV-E160S", // Galaxy Note
    "SGH-I717", // Galaxy Note
    "SC-05D", // Galaxy Note
    "SGH-T879", // Galaxy Note
    "GT-I9220", // Galaxy Note
    "GT-I9228", // Galaxy Note
    "SCH-I889", // Galaxy Note
    "SGH-I717", // Galaxy Note
};

static {
    Arrays.sort(LAUNCHER_ALTERNATIVE_EXCEPTIONS);
}


public static boolean isAlternativeMargins(Context context) {
    if (Build.VERSION.SDK_INT < 14)
        return false;

    PackageManager pm = context.getPackageManager();
    Intent i = new Intent(Intent.ACTION_MAIN);
    i.addCategory(Intent.CATEGORY_HOME);
    i.addCategory(Intent.CATEGORY_DEFAULT);

    ResolveInfo info = pm.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY);

    if (
            info != null && info.activityInfo != null && 
            info.activityInfo.taskAffinity != null
    ) {
        int pos = Arrays.binarySearch(
                LAUNCHER_ALTERNATIVES, info.activityInfo.taskAffinity
        );

        // if TouchWiz
        if (pos >= 0) {
            // if >= Android 4.1.2 and (Galaxy S II or Galaxy Note) 
            if ( getSdk() >= 16 && Arrays.binarySearch(
                    LAUNCHER_ALTERNATIVE_EXCEPTIONS, Build.MODEL
            ) >= 0)
                return false;

            return true;
        }
    }
    return false;
}

The code above detects the default launcher and returns true, if it is TouchWiz.

EDIT: Modified for additional launchers that seem to have the same problem as TouchWiz.

EDIT 2: Modified for Android 4.1.2 on Galaxy S II and Galaxy Note where the widget margins work as expected. In contrast on a Galaxy S 3 with Android 4.1.2, they don't work as expected which will probably change for future TouchWiz versions from Samsung.