[英]Handling orientation changes 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);
這取決於運行該代碼的時間,但是通常不需要執行任何特殊操作。
當Android檢測到方向改變時,系統將重新創建您的“活動”,並有機會根據新配置進行onCreate,重新布局以及重新渲染所有內容。
使用android android:configChanges
應該謹慎使用,而不是您想到的第一個選項,因為配置更改后,系統會在為您選擇合適的資源方面做得更好(並且您必須處理該代碼路徑其他形式的配置仍然會發生變化)。
請參閱: http : //developer.android.com/guide/topics/resources/runtime-changes.html
是。 一種方法是將android:configChanges="orientation|keyboardHidden"
放在AndroidManifest.xml文件的<activity>
上,然后重寫onConfigurationChanged
來檢測方向變化。
如果要保留一行中顯示的圖像數量,可以使用GridView 。 這樣,您可以控制每行的列數。
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.