In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how Android programs are compatible with phones and tablets". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Let's take a look at the Android phone settings interface. Click Sound to jump to the sound settings interface, as shown in the following two images:
Then take a look at the settings interface of Android Pad. Both the main settings page and the sound settings page are displayed in one interface, as shown in the following figure:
If this is the effect of two different App, it's not surprising. But if it's the same App, running on the phone and on the tablet has the above two effects, are you already attracted? Let's simulate and implement it now.
First of all, you need to know something about Fragment. If you haven't been exposed to Fragment, it is recommended to read this article on full Android Fragment parsing, everything you need to know about fragments. And this code is running on Android version 4.0, if your SDK version is still relatively low, it is recommended to upgrade first.
Create a new Android project and name it FragmentDemo. Open or create a new MainActivity as the main Activity of the program, with the following automatically generated content:
Publicclass MainActivity extends Activity {
@ Override
Publicvoid onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
SetContentView (R.layout.activity_main)
}
}
Public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main);}}
As a veteran of Android, the above code is so trivial that there is such code in every Activity. But today our program will not be so simple, loading the layout of this part is still a lot of work.
Open or create a new res/layout/activity_main.xml as the main layout file of the program, with the following code:
This layout refers to a MenuFragment, which we will implement later. Let's take a look at a key point today. We need to create a new activity_main.xml. The layout file name is the same as the previous main layout file name, but under a different directory.
Don't go away. The next page will introduce you to Fragment's realization of tablet compatibility with Android apps.
Create a new layout-large directory under the res directory, and then create a new activity_main.xml under this directory, and add the following code:
This layout also references MenuFragment, and a FrameLayout is added to display the details. In fact, it corresponds to the left layout and the right layout on the flat interface, respectively.
The technique of dynamically loading layouts is used here. First, setContentView (R.layout.activity_main) is called in Activity to indicate that the current Activity wants to load the layout file activity_main. And the Android system will judge whether the program is running on the big screen device according to the current running environment. If the running line is on the big screen device, it will load the activity_main.xml under the layout-large directory, otherwise it will load the activity_main.xml under the layout directory by default.
For more information about dynamically loading layouts, you can read this article officially provided by Android to support all methods for different screen sizes.
Let's implement the long-lost MenuFragment and create a new MenuFragment class that inherits from Fragment. The specific code is as follows:
Publicclass MenuFragment extends Fragment implements OnItemClickListener {
/ * *
* only one ListView is included in the menu interface.
, /
Private ListView menuList
/ * *
* Adapter for ListView.
, /
Private ArrayAdapter adapter
/ * *
* only two pieces of data are simply used to fill in the data of ListView.
, /
Private String [] menuItems = {"Sound", "Display"}
/ * *
* whether it is in two-page mode. If an Activity contains two Fragment, it is two-page mode.
, /
Privateboolean isTwoPane
/ * *
* initialize the data in the adapter when Activity and Fragment establish an association.
, /
@ Override
Publicvoid onAttach (Activity activity) {
Super.onAttach (activity)
Adapter = new ArrayAdapter (activity, android.R.layout.simple_list_item_1, menuItems)
}
/ * *
* load the menu_fragment layout file, bind the adapter for ListView, and set listening events.
, /
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate (R.layout.menu_fragment, container, false)
MenuList = (ListView) view.findViewById (R.id.menu_list)
MenuList.setAdapter (adapter)
MenuList.setOnItemClickListener (this)
Return view
}
/ * *
* when the Activity is created, try to get whether there is an element of details_layout in the layout file. If so, it indicates that the current
* it is in two-page mode, if it is not stated that it is currently in single-page mode.
, /
@ Override
Publicvoid onActivityCreated (Bundle savedInstanceState) {
Super.onActivityCreated (savedInstanceState)
If (getActivity () .findViewById (R.id.details_layout)! = null) {
IsTwoPane = true
} else {
IsTwoPane = false
}
}
/ * *
* when dealing with the click event of ListView, it will be judged according to whether it is in two-page mode. If it is in two-page mode, Fragment is added dynamically.
* if it is not in two-page mode, a new Activity is opened.
, /
@ Override
Publicvoid onItemClick (AdapterView arg0, View view, int index, long arg3) {
If (isTwoPane) {
Fragment fragment = null
If (index = = 0) {
Fragment = new SoundFragment ()
} elseif (index = = 1) {
Fragment = new DisplayFragment ()
}
GetFragmentManager (). BeginTransaction (). Replace (R.id.details_layout, fragment). Commit ()
} else {
Intent intent = null
If (index = = 0) {
Intent = new Intent (getActivity (), SoundActivity.class)
} elseif (index = = 1) {
Intent = new Intent (getActivity (), DisplayActivity.class)
}
StartActivity (intent)
}
}
}
The public class MenuFragment extends Fragment implements OnItemClickListener {/ * menu interface contains only one ListView. Adapter for * / private ListView menuList; / * ListView. * / private ArrayAdapter adapter; / * is used to populate the data of ListView. Here we simply use only two pieces of data. * / private String [] menuItems = {"Sound", "Display"}; / * whether it is in two-page mode. If an Activity contains two Fragment, it is two-page mode. * / private boolean isTwoPane; / * * initialize the data in the adapter when the Activity is associated with the Fragment. * / @ Override public void onAttach (Activity activity) {super.onAttach (activity); adapter = new ArrayAdapter (activity, android.R.layout.simple_list_item_1, menuItems);} / * load the menu_fragment layout file, bind the adapter for ListView, and set listening events. * / @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate (R.layout.menu_fragment, container, false); menuList = (ListView) view.findViewById (R.id.menu_list); menuList.setAdapter (adapter); menuList.setOnItemClickListener (this); return view } / * when the Activity is created, try to find out whether there is a details_layout element in the layout file. If it indicates that the current * is in two-page mode, if it does not indicate that it is in single-page mode. * / @ Override public void onActivityCreated (Bundle savedInstanceState) {super.onActivityCreated (savedInstanceState); if (getActivity (). FindViewById (R.id.details_layout)! = null) {isTwoPane = true;} else {isTwoPane = false;}} / * handles the click event of ListView and determines whether it is currently in two-page mode. If it is in two-page mode, Fragment is added dynamically. * if it is not in two-page mode, a new Activity is opened. * / @ Override public void onItemClick (AdapterView arg0, View view, int index, long arg3) {if (isTwoPane) {Fragment fragment = null; if (index = = 0) {fragment = new SoundFragment ();} else if (index = = 1) {fragment = new DisplayFragment () } getFragmentManager (). BeginTransaction (). Replace (R.id.details_layout, fragment). Commit ();} else {Intent intent = null; if (index = = 0) {intent = new Intent (getActivity (), SoundActivity.class);} else if (index = = 1) {intent = new Intent (getActivity (), DisplayActivity.class) } startActivity (intent);}
The code for this class is not long, so let me explain it briefly. The menu_fragment layout is loaded in the onCreateView method, which contains a ListView, and then we populate the ListView with two simple data "Sound" and "Display". Another judgment is made in the onActivityCreated method: if the layout of the Activity contains the element details_layout, then it is currently in two-page mode, otherwise it is in single-page mode. The onItemClick method handles the click event of ListView and finds that if it is in two-page mode, it dynamically adds Fragment to the details_layout, and if it is in single-page mode, it opens the new Activity directly.
Don't go away. The next page will introduce you to Fragment's realization of tablet compatibility with Android apps.
We add everything else referenced in MenuFragment one by one. Create a new menu_fragment.xml file and add the following code:
Then create a new SoundFragment, which is very simple:
Publicclass SoundFragment extends Fragment {
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate (R.layout.sound_fragment, container, false)
Return view
}
}
Public class SoundFragment extends Fragment {@ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate (R.layout.sound_fragment, container, false); return view;}}
Here SoundFragment needs to use the sound_fragment.xml layout file, so here we create the layout file and add the following code:
AtiveLayoutxmlns:androidRelativeLayoutxmlns:android= "http://schemas.android.com/apk/res/android"
Android:layout_width= "match_parent"
Android:layout_height= "match_parent"
Android:background= "# 00ff00"
Android:orientation= "vertical" >
By the same token, let's create new DisplayFragment and display_fragment.xml layout files:
Publicclass DisplayFragment extends Fragment {
Public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate (R.layout.display_fragment, container, false)
Return view
}
}
Then create a new SoundActivity with the following code:
Publicclass SoundActivity extends Activity {
@ Override
Protectedvoid onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
SetContentView (R.layout.sound_activity)
}
}
Public class SoundActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.sound_activity);}}
This Activity just loads a layout file. Now let's implement the layout file sound_activity.xml:
This layout file references SoundFragment, and the advantage of writing this is that later we only need to modify the code in SoundFragment, and SoundActivity will automatically change, because all its code is referenced from SoundFragment.
OK, in the same way, let's finish the DisplayActivity:
Publicclass DisplayActivity extends Activity {
@ Override
Protectedvoid onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
SetContentView (R.layout.display_activity)
}
}
Public class DisplayActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.display_activity);}}
Don't go away. The next page will introduce you to Fragment's realization of tablet compatibility with Android apps.
Then add display_activity.xml:
Now that all the code is complete, let's take a look at the effect.
First, run the program on the mobile phone, and the effect picture is as follows:
Click Sound and Display, respectively, and the interface will jump to the sound and display interface:
Then run the program on the tablet, click Sound, and the effect is as follows:
Then click Display to switch to the display screen, and the effect is as follows:
This is the end of the content of "how Android programs are compatible with phones and tablets". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.