2017-04-19 55 views
1
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Display; 
import android.view.WindowManager; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     FragmentManager fm=getFragmentManager(); 
     FragmentTransaction ft= fm.beginTransaction(); 


     WindowManager wm= getWindowManager(); 
     Display d=wm.getDefaultDisplay(); 

     if (d.getWidth()>d.getHeight()) 
     { 
      Fragment1 f1 = new Fragment1(); 
      ft.replace(android.R.id.content, f1); 
     } 
     else 
     { 
      Fragment2 f2 = new Fragment2(); 
      ft.replace(android.R.id.content, f2); 
     } 
    ft.commit(); 
    } 
} 

我試圖使用Android的碎片和希望顯示fragment1時顯示的寬度較大比顯示屏的高度和fragment2當顯示屏的高度大於顯示屏的寬度,但是當使用getWidth()getHeight()時,android studio會說這些方法已被刪除。 那麼如何知道什麼時候寬度較大,何時高度較高?我應該到位的getWidth()和getHeight()的使用,知道什麼時候寬度較大,當高度大於

+1

你試圖在肖像上顯示片段1而在橫向上顯示片段2 –

+0

@Shararti KAKA是的....我試圖做同樣的事情 – Ankit

+0

使用'DisplayMetrics' –

回答

3

使用DisplayMetrics而不是Display

試試這個:

import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Display; 
import android.view.WindowManager; 
import android.util.DisplayMetrics; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     FragmentManager fm=getFragmentManager(); 
     FragmentTransaction ft= fm.beginTransaction(); 

     DisplayMetrics displayMetrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
     int height = displayMetrics.heightPixels; 
     int width = displayMetrics.widthPixels; 

     if (width > height) 
     { 
      Fragment1 f1 = new Fragment1(); 
      ft.replace(android.R.id.content, f1); 
     } 
     else 
     { 
      Fragment2 f2 = new Fragment2(); 
      ft.replace(android.R.id.content, f2); 
     } 

     ft.commit(); 
    } 
} 
+0

@Ankit請仔細閱讀此:http://stackoverflow.com/help/someone-answers – FAT

+0

先生,我正在檢查剔號,但該網站發出警告,我只能在10分鐘後檢查剔號,所以我正在爲此付費 – Ankit

+0

您已接受此答案。謝謝:)請務必稍後再進行投票。 – FAT

1

使用此來獲得寬度和高度。

int Measuredwidth = 0; 
int Measuredheight = 0; 
Point size = new Point(); 
WindowManager wm = getWindowManager(); 

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
    wm.getDefaultDisplay().getSize(size); 
    Measuredwidth = size.x; 
    Measuredheight = size.y; 
}else{ 
    Display d = w.getDefaultDisplay(); 
    Measuredwidth = d.getWidth(); 
    Measuredheight = d.getHeight(); 
} 
+0

這是x和y來自 – Ankit

+0

這是顯示x和y的像素尺寸 –

3

您可以使用DisplayMetrics

DisplayMetrics displaymetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
int height = displaymetrics.heightPixels; 
int Width = displaymetrics.widthPixels; 

if (Width > height) 
    { 
     Fragment1 f1 = new Fragment1(); 
     ft.replace(android.R.id.content, f1); 
    } 
    else 
    { 
     Fragment2 f2 = new Fragment2(); 
     ft.replace(android.R.id.content, f2); 
    } 
+0

感謝您的答案和寶貴的時間 – Ankit

2
DisplayMetrics displaymetrics = new DisplayMetrics(); 
mContext.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
int screenWidth = displaymetrics.widthPixels; 
int screenHeight = displaymetrics.heightPixels; 
1

嘗試

context.getWindowManager().getDefaultDisplay().getMetrics(dm); 
if(dm.widthPixels > dm.heightPixels) { 
    //width is greater, i.e. landscape 
    Fragment1 f1 = new Fragment1(); 
     ft.replace(android.R.id.content, f1); 
} else { 
    //height is greater i.e. portrait 
    Fragment2 f2 = new Fragment2(); 
    ft.replace(android.R.id.content, f2); 
} 

您可以直接通過重寫onConfigurationChanges在檢測方向MainActivity

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

    // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     Fragment1 f1 = new Fragment1(); 
     ft.replace(android.R.id.content, f1); 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     Fragment2 f2 = new Fragment2(); 
     ft.replace(android.R.id.content, f2); 
    } 
} 

//Add configChanges in manifest 
<activity android:name=".MainActivity" 
     android:configChanges="orientation|keyboardHidden" 
     android:label="@string/app_name"> 
相關問題