I want to support both small screens and large screens
I have an image view in my layout - that in small screens it needs a layout height of "wrap_content"
and in large screens it needs 400dp (wrap content is too small)
instead of creating another layout, i wanted to create a dimension
<dimen name="layout_height">wrap_content</dimen>
<dimen name="layout_height">400dp</dimen>
and assign them to the right folders and in my imageview write
layout_height=@dimen/layout_height
is this possible in any way, without creating another layout ?
-
in some layouts "wrap_content" is neededLena Bru– Lena Bru2014年06月19日 08:16:10 +00:00Commented Jun 19, 2014 at 8:16
4 Answers 4
To assign wrap_content or match_parent from resources you need to create these items in dimens.xml file:
<item name="match_parent" format="integer" type="dimen">-1</item>
<item name="wrap_content" format="integer" type="dimen">-2</item>
Then you can simply use it like this:
<dimen name="layout_height">@dimen/wrap_content</dimen>
you can have a different style for small and large screen, overriding the layout_height property. Unfortunately you can not set wrap_content inside dimen
1 Comment
APPROACH 1 - Doing it programmatically:
STEP 1. Add the following to the onCreate() of your Activity:
ImageView i = (ImageView)findViewById(R.id.myimageview);
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
i.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,dpi(400)));
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
i.setLayoutParams(new ImageView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
i.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
break;
}
STEP 2. The function dpi() is defined as below:
private int dpi(int i) {
int value = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, i,
getResources().getDisplayMetrics());
return value;
}
NOTE: Make sure you don't set android:layout_height and android:layout_width for this ImageView in your XML layout.
APPROACH 2 - Doing through XML:
In your /res directory, make two folders layout-small and layout-large. In these two folders, put the same XML layout file and manually set android:layout_width="wrap_content" in the XML file in the layout-small folder and android:layout_height="400dp" in the XML file in the layout-large folder. The existing layout folder will contain the XML layout for normal sized screens.
References:
1 Comment
you can use layout_height and minHeight at the time. And for the small screen, set minHeight as 0dp, however, for the big screen, set it as 400dp. Always set layout_height as warp_content.
I think this will work for your case.