How to change view for multiple window display in

2019-05-28 18:38发布

问题:

How can we give separate layout or separate activity for Multiple window ?

eg. I have checked below things with help of android developer site

<activity android:name="com.configure.it.MyScreen">
    <layout android:defaultHeight="400dp"
          android:defaultWidth="200dp"
          android:gravity="top|end"
          android:minimalSize="300dp" />
</activity>

by declaring above things it affect how an activity behaves in multi-window mode.

But how can I show different layout if my particular screen is activated on Multiple-Window ?

回答1:

From the Android Developer link .

To make changes in UI or separate layout which should be used on Multiple-window activate.

We can check if activity is in Multiple-window by following way

  • From activity Activity.isInMultiWindowMode() Call to find out if the activity is in multi-window mode.

eg. To check in Activity if its multiple window than header(or any view should have Red background color if its not in multiple window thn it should be Green background color)

headerView.setBackgroundColor(inMultiWindow()?Color.RED:Color.GREEN);

using inMultiWindow() replacing Fragment is also possible

  • To get Callback on Multiple-Window Activation.

From Activity onMultiWindowChanged method is available to handle runtime changes on this method callback.System will give callback on this method whenever the activity goes into or out of multi-window mode with boolean value.With the help of sample link & android developer link

@Override
public void onMultiWindowChanged(boolean inMultiWindow) {
    super.onMultiWindowChanged(inMultiWindow);
    headerView.setBackgroundColor(inMultiWindow ? Color.RED : Color.GREEN);
    // here we can change entire fragment also.
    //If multiple window in than seperate and for multiple window out different

}

Will keep updating if I get anything else.



回答2:

isInMultiWindowMode() is added in API 24 to check device is in Multi window Mode or not, it returns a boolean value. whenever device goes to Multi window Mode it triggers onConfigurationChanged() method.

you will need to manually update your Views, Reload resources, etc... in onConfigurationChanged() based on Landscape and portrait mode.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
     if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
      {
        //do something (Update your views / Reload resources)
      }else{

      }
}

In Manifest.xml

 <activity
   android:name=".yourActivity"
   android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
 />

For further reference check with Multi-Window Support Google Dev and MultiWindow Medium Corp