我正在为平板电脑开发一个Android应用程序,其中我有一个场景,我必须在大型设备中使用导航抽屉显示菜单,并在x大型设备显示菜单中作为布局,并且没有导航抽屉。谁能提出一些关于如何做到这一点的想法?
答案 0 :(得分:2)
使用不同的布局文件(包含导航抽屉xml代码)和x-large(不包含导航抽屉xml代码)设备。
在你的java代码中,
DrawerLayout mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
if (mDrawerLayout != null) {
//Checking for null will make sure that there is no null pointer exception in the x-large screens where there is no nav drawer layout.
//Add a similar null check for mDrawerLayout wherever you are using the mDrawerLayout variable.
// Setup ActionBar Icon.
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ActionBarDrawerToggle ties together the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
// Set the title on the action when drawer open
getSupportActionBar().setTitle(mDrawerTitle);
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
答案 1 :(得分:0)
是的,你可以使用屏幕尺寸来实现这一点。你必须为手机和平板电脑屏幕做出不同的布局。就像高达5.7“的设备进入phablet意味着手机。你必须使用布局文件夹。然后来7英寸平板电脑你必须分别为横向和纵向模式创建一个文件夹 layout-sw600dp-land 和 layout-sw600dp-port 。对于10英寸设备你必须创建一个文件夹横向和纵向模式的 layout-sw720dp-land 和 layout-sw720dp-port 。这个是关于布局的Xml设计。
检查启动画面上的设备大小(如果它在您的项目中,请使用此代码,请设置内容视图)并将其转换为英寸,如下所示:
try {
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int displayHeight = displaymetrics.heightPixels;
int displayWidth = displaymetrics.widthPixels;
double x = Math.pow(displaymetrics.widthPixels
/ displaymetrics.xdpi, 2);
double y = Math.pow(displaymetrics.heightPixels
/ displaymetrics.ydpi, 2);
double screenInches = Math.sqrt(x + y);
Log.d("debug", "Screen inches : " + screenInches);
int _deviceinch = (int) (screenInches);
} catch (Exception e) {
e.printStackTrace();
}
并检查设备尺寸并为您的设备设置内容视图,如:
if (a <= 6) {
setContentView(R.layout.phone_all_products);
} else if (a >= 7 && a < 9) {
setContentView( R.layout.seven_inch_all_products);
System.out.println("in seven inch");
} else if (a >= 9) {
setContentView(R.layout.ten_inch_all_products);
System.out.println("in ten inch");
}