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
{1}}和getWidth()
安卓工作室正在说这些方法已被删除。
那么如何知道宽度何时更大以及何时高度更大?
答案 0 :(得分:4)
使用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();
}
}
答案 1 :(得分: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);
}
答案 2 :(得分:2)
alert(elems2.length)
答案 3 :(得分: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();
}
答案 4 :(得分: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">