不同屏幕尺寸的样式可以不同吗? [英] Can styles be different for different screen sizes?

查看:61
本文介绍了不同屏幕尺寸的样式可以不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个GridView,我想根据当前屏幕宽度手动设置列数.

我试图做的是设置一种应应用于GridView的样式,并在 values-w600dp 文件夹中提供一个替代样式,并使用 android:numColumns 属性设置为其他值.这没用.

我知道有多种方法可以实现我最初提出的目标(例如,通过声明整数资源并以通用样式使用它),其中将列宽设置为一个值,并允许GridView自动确定列数可能是最理智的解决方案.

但是,我仍然想知道为什么我最初的尝试不起作用.在文档中,我发现带有样式的XML文件可以命名为任何名称,只要将其放置在 res/values 目录中即可.对于字符串和字符串都是可本地化的,也有类似的说法,所以我不知道文档是否说它不起作用,或者是否意味着可以用不同的 res/values -... 目录,例如字符串.

所以问题是:可以为不同的配置提供样式吗?

文件已发布

res/values/styles.xml

 <?xml version ="1.0" encoding ="utf-8"?><资源>< style name ="MyGridView">< item name ="android:numColumns"> 1</item></style></resources> 

res/values-w400dp/styles.xml

 <?xml version ="1.0" encoding ="utf-8"?><资源>< style name ="MyGridView">< item name ="android:numColumns"> 2</item></style></resources> 

res/layout/grid_fragment.xml

 <?xml version ="1.0" encoding ="utf-8"?>< RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"android:layout_width ="fill_parent"android:layout_height ="fill_parent"android:paddingTop =?android:attr/actionBarSize">< GridViewandroid:id ="@ + id/gridview"android:layout_width ="fill_parent"android:layout_height ="fill_parent"android:verticalSpacing ="12dp"android:paddingTop ="12dp"android:paddingBottom ="12dp"android:clipToPadding ="false"android:listSelector ="@ drawable/list_selector"android:drawSelectorOnTop ="true"style ="@ style/MyGridView"/></RelativeLayout> 

当我粘贴代码时,我发现了问题.我在布局XML中犯了一个愚蠢的错误: style ="MyGridView" .

解决方案

是的,但有一些警告.

默认情况下,Android会在加载时根据当前条件加载适当的资源;有关更多信息,请参见 Android资源文档.基本上,资源文件夹的名称决定了何时加载它们,因此您可以拥有文件夹

 /值/values-sw50dp(最小宽度50dp,适用于大于50dp的屏幕)/values-car(如果您在汽车停靠站中) 

等您可以使用许多选项,例如 screensize 用于一般感觉,或可用宽度用于根据宽度进行选择.

但是.

仅当您未在清单中为该活动设置任何 configChanges 时,此方法才有效:如果您有设置,Android会假设您自己在处理一切,并且不会自动加载正确的资源.另外,一般来说,请遵循良好的代码意识,不要太过分,为15个不同的屏幕宽度中的每一个都制作一个单独的布局文件.

I have a GridView in my app, and I would like to manually set the number of columns based on the current screen width.

What I attempted to do was set up a style that should be applied to the GridView, and provide an alternative style in the values-w600dp folder, with the android:numColumns attribute set to a different value. This didn't work.

I'm aware there are different ways to achieve what I initially proposed (for example by declaring an integer resource and use it in the common style), of which setting the column width to a value and allowing the GridView to automatically determine the number of columns would probably be the most sane solution.

However, I'm still left wondering why my initial attempt didn't work. In the documentation I found that the XML file with the styles can be named anything, as long as its placed in the res/values directory. Something similar is also said for strings and strings are localizable, so I don't know if the docs say it won't work or if it means that it could be provided in different res/values-... directories, like strings.

So the question is: can styles be provided for different configurations?

Edit: Files posted

res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyGridView">
        <item name="android:numColumns">1</item>
    </style>
</resources>

res/values-w400dp/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyGridView">
        <item name="android:numColumns">2</item>
    </style>
</resources>

res/layout/grid_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="?android:attr/actionBarSize"
    >
    <GridView
            android:id="@+id/gridview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:verticalSpacing="12dp"
            android:paddingTop="12dp"
            android:paddingBottom="12dp"
            android:clipToPadding="false"
            android:listSelector="@drawable/list_selector"
            android:drawSelectorOnTop="true"
            style="@style/MyGridView"
            />
</RelativeLayout>

While I was pasting the code, I found the problem. I had a silly mistake in the layout XML: style="MyGridView".

解决方案

Yes, with some caveats.

Android, by default, will load the appropriate resource based on the current conditions at load time; see the Android Docs on Resources for more. Basically, the name of your resource folders determines when they should load, so you could have folders

/values
/values-sw50dp  (smallest width 50dp, for screens bigger than 50dp)
/values-car     (if you were in a car dock)

etc. There are lots of options you can use, like screensize for general feels, or available width to do it based on width.

HOWEVER.

This only works if you aren't setting any configChanges for that Activity in your manifest: if you do have some set, Android assumes you're handling everything yourself and won't autoload the correct resource. Also, follow good code sense in general and don't go overboard, making a separate layout file for each of 15 different screen widths.

这篇关于不同屏幕尺寸的样式可以不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆