简体   繁体   English

片段中的操作栏-平板电脑和手机

[英]Action Bar in Fragments - Tablets and Phones

I am creating an App where I am using Fragments so as to attain UI flexibility. 我正在创建一个使用片段的应用程序,以实现UI灵活性。

In the Phone Portrait and Landscape mode the Fragmnets Appears like this : 在“电话纵向和横向”模式下,fragmnets的外观如下: 手机风景和肖像

In Tablets Portrait and Landscape Mode it Appears like this : 在Tablets纵向和横向模式下,其显示如下:

平板电脑肖像和风景

My main concern is ACTION BAR. 我主要关心的是ACTION BAR。 I want to hide action bar only in case of SMARTPHONE in LANDSCAPE mode where FRAGMENT B appears Full Screen. 我只想在“片段B”显示为“全屏”的LANDSCAPE模式下隐藏SMARTPHONE的操作栏。 I want to display the Action Bar in all other cases as I have shown in the Images. 我想在所有其他情况下都显示操作栏,如我在图像中所示。

I have searched whole stack overflow but did not find any solution. 我搜索了整个堆栈溢出,但未找到任何解决方案。 Can any one help me? 谁能帮我? Thanks in advance. 提前致谢。

To solve this, there are 2 things you need to do: 要解决此问题,您需要做两件事:

  1. Check if your device is a smartphone or tablet. 检查您的设备是智能手机还是平板电脑。

Create a new resources folder called values-sw600dp inside your res folder. res文件夹内创建一个名为values-sw600dp的新资源文件夹。 (This only works for Android versions before 3.2, for previous versions create a values-xlarge folder) (这仅适用于3.2之前的Android版本,对于以前的版本,请创建values-xlarge文件夹)

Then here we create a file and set the following value: 然后在这里我们创建一个文件并设置以下值:

<resources>
    <bool name="isTablet">true</bool>
</resources>

Then, in the standard value file (as res/values/), you set such boolean to false: 然后,在标准值文件(如res / values /)中,将此类布尔值设置为false:

false

Now you can check if you are running in a tablet size device: 现在,您可以检查您是否在平板电脑大小的设备中运行:

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
// do a
} else {
// do b
}
  1. In case of being a smartphone, check orientation and hide action bar. 如果是智能手机,请检查方向并隐藏操作栏。

In your onCreateView method in your fragmentB after checking if you are using a smartphone or tablet (using your-new-resource-file.xml) we have to check the orientation. 在检查您是否使用智能手机或平板电脑(使用your-new-resource-file.xml)之后,在fragmentB onCreateView方法中,我们必须检查方向。 If the orientation is LANDSCAPE, then we hide the actionbar. 如果方向是LANDSCAPE,则我们隐藏操作栏。

 boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (!tabletSize) 
{
  //Lets check orientation
  if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
  {
      getActivity().getActionBar().hide(); //getSupportActionBar().hide() if using appcompat
  }
}

Hope it helps 希望能帮助到你

I have used this codes to check if we are in SMARTPHONE or TABLET : 我已使用此代码检查我们是在SMARTPHONE还是TABLET中:

// Function to check if we are inside Phone or Tablet
public static boolean isTablet(Context context) {

    Boolean tablet = (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;

    return tablet;

    /* Returns TRUE if we are in TABLET
     * 
     * Returns FALSE if we in PHONE
     * 
     * 
     * */
}

Then after if i am inside the phone : I have used following code to check my screen orientation: 然后,如果我在手机内:我使用以下代码检查屏幕方向:

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
  getActivity().getActionBar().hide(); //getSupportActionBar().hide() if using appcompat Library
}else{
  getActivity().getActionBar().show(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM