2011-11-24 176 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" 
     />