2011-11-24 85 views
0

我有一个表包含每行的图像数量。每行图像的数量根据图像宽度和屏幕宽度实时决定。当我正常使用手机时,图像会在手机屏幕上分开。当手机方向改变时,所有图像都出现在同一行上。我是否应该明确处理这种情况下的方向更改?处理方向变化android

// get the number of images per column based on the screen 
// resolution 
Display display = getWindowManager().getDefaultDisplay(); 
int screenWidth = display.getWidth(); 

Resources res = getResources(); 
Drawable drawable = res.getDrawable(R.drawable.emo_im_happy); 

numberOfImagesPerRow = screenWidth/(drawable.getIntrinsicWidth() + 10); 

int numberOfRows = (int) Math.ceil(numberOfEmotions 
        /numberOfImagesPerRow); 

回答

3

它取决于代码运行的时间,但通常不需要做任何特殊的事情。

当Android检测到方向更改时,系统将重新创建您的活动,并且它将有机会通过onCreate并根据新配置重新布局和重新渲染所有内容。

使用android android:configChanges应谨慎使用,而不是您想象的第一个选项,因为在配置更改后系统会为您选择合适的资源做得更好(并且您必须处理该代码无论如何,其他形式的配置改变的路径)。

请参见: http://developer.android.com/guide/topics/resources/runtime-changes.html

0

是的。一种方法是将android:configChanges="orientation|keyboardHidden"放在AndroidManifest.xml文件的<activity>中,然后覆盖onConfigurationChanged以检测方向更改。

0

如果你想坚持一个行,你可以使用GridView控件显示的图像的数量。通过这种方式,您可以控制每行的列数。

<GridView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:isScrollContainer="true" 
     android:numColumns="3" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     />