2013-05-10 69 views
0

如何在方向更改時更改android中的對話框的尺寸。在的onCreate更改方向更改時自定義對話框的尺寸

static final float[] DIMENSIONS_DIFF_LANDSCAPE = {20, 60}; 
static final float[] DIMENSIONS_DIFF_PORTRAIT = {40, 60}; 

final float scale = getContext().getResources().getDisplayMetrics().density; 
int orientation = getContext().getResources().getConfiguration().orientation; 

float[] dimensions = 
       (orientation == Configuration.ORIENTATION_LANDSCAPE) 
         ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT; 
       addContentView(mContent, new LinearLayout.LayoutParams(
        display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)), 
        display.getHeight() - ((int) (dimensions[1] * scale + 0.5f)))); 

代碼我想做同樣的,當方向改變。請幫助

回答

0

我建議你創建一個init方法,並把你想要在onCreate和方向變化期間發生的東西。

首先,你應該添加到您的活動XML

android:configChanges="keyboardHidden|orientation"

然後實現方法

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    static final float[] DIMENSIONS_DIFF_LANDSCAPE = {20, 60}; 
    static final float[] DIMENSIONS_DIFF_PORTRAIT = {40, 60}; 

    final float scale = getContext().getResources().getDisplayMetrics().density; 

    float[] dimensions = 
       (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
         ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT; 
       addContentView(mContent, new LinearLayout.LayoutParams(
        display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)), 
        display.getHeight() - ((int) (dimensions[1] * scale + 0.5f)))); 
} 

但你還是應該做的是對的onCreate了。

1

只是檢查了這一點,它的自定義代碼來設置對話框高度根據oriebtation:它通過實現這個我的對話成爲靈活的工作對我來說...我M在平板電腦上使用這個..

WindowManager winMan = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
      int height = winMan.getDefaultDisplay().getHeight(); 
      int width=winMan.getDefaultDisplay().getWidth(); 
      if(height>width){ 

       if(DeviceType.getDeviceType(context)==Device.NEXUS7){ 
       dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,1100)); 
       }else if (DeviceType.getDeviceType(context)==Device.GALAXYTAB2){ 
        dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(580,900)); 
       }else if(DeviceType.getDeviceType(context)==Device.GALAXY10){ 
        dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,1100)); 
       } 


      else{ 
       if(DeviceType.getDeviceType(context)==Device.NEXUS7){ 
        dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,700)); 
       }else if (DeviceType.getDeviceType(context)==Device.GALAXYTAB2){ 
        dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,500)); 
        System.out.println("galaxy tab 2 landescape"); 
       }else if(DeviceType.getDeviceType(context)==Device.GALAXY10){ 
       dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,700)); 
      }