我应该用什么代替 getWidth() 和 getHeight() 来知道什么时候宽度更大,什么时候高度更大

What should I use in place of getWidth() and getHeight() to know when width is greater and when height is greater

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 工作室时说这些方法已被描述。 那怎么知道什么时候宽大,什么时候高大呢?

使用它来获取宽度和高度。

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(); 
}

您可以使用 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);
    }

使用 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();
    }
}
DisplayMetrics displaymetrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;

尝试

 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);
 }

您可以通过覆盖 MainActivity

中的 onConfigurationChanges 直接检测方向
@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">