问题
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"
.
回答1:
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.
来源:https://stackoverflow.com/questions/20983814/can-styles-be-different-for-different-screen-sizes