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会提示这些方法已经过时。那么如何知道何时宽度更大,何时高度更大?
DisplayMetrics
- IntelliJ Amiya