eth0 在系统启动时自动激活
2010-08-13 10:39
281 查看
To create a dynamic and multi-pane user interface on Android, you need to encapsulate UI components and activity behaviors into modules that you can swap into and out of your activities. You can create these modules with the
Note: If you decide that the minimum API level your app requires is 11 or higher, you don't need to use the Support Library and can instead use the framework's built in
Figure 1. Two fragments, displayed in different configurations for the same activity on different screen sizes. On a large screen, both fragments fit side by side, but on a handset device, only one fragment fits at a time so the fragments must replace each other as the user navigates.The
Fragmentclass, which behaves somewhat like a nested activitythat can define its own layout and manage its own lifecycle.When a fragment specifies its own layout, it can be configured in different combinations with other fragments inside an activity to modify your layout configuration for different screen sizes (a small screen might show one fragment at a time, but a large screen can show two or more).This class shows you how to create a dynamic user experience with fragments and optimize your app's user experience for devices with different screen sizes, all while continuing to support devices running versions as old as Android 1.6.
Creating a Fragment
You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities). This lesson shows how to extend theFragmentclass using theSupport Library so your app remains compatible with devices running system versions as low as Android 1.6.
Note: If you decide that the minimum API level your app requires is 11 or higher, you don't need to use the Support Library and can instead use the framework's built in
Fragmentclass and related APIs. Just be aware that this lesson is focused on using the APIs from the Support Library, which use a specific package signature and sometimes slightly different API names than the versions included in the platform.Before you begin this lesson, you must set up your Android project to use the Support Library. If you have not used the Support Library before, set up your project to use the v4 library by following the Support Library Setupdocument. However, you can also include the action barin your activities by instead using the v7 appcompat library, which is compatible with Android 2.1 (API level 7) and also includes the
FragmentAPIs.
Create a Fragment Class
To create a fragment, extend theFragmentclass, then override key lifecycle methods to insert your app logic, similar to the way you would with an
Activityclass.One difference when creating a
Fragmentis that you must use the
onCreateView()callback to define the layout. In fact, this is the only callback you need in order to get a fragment running. For example, here's a simple fragment that specifies its own layout:
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.ViewGroup; public class ArticleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.article_view, container, false); } }Just like an activity, a fragment should implement other lifecycle callbacks that allow you to manage its state as it is added or removed from the activity and as the activity transitions between its lifecycle states. For instance, when the activity's
onPause()method is called, any fragments in the activity also receive a call to
onPause().More information about the fragment lifecycle and callback methods is available in the Fragments developer guide.
Add a Fragment to an Activity using XML
While fragments are reusable, modular UI components, each instance of aFragmentclass must be associated with a parent
FragmentActivity. You can achieve this association by defining each fragment within your activity layout XML file.Note:
FragmentActivityis a special activity provided in the Support Library to handle fragments on system versions older than API level 11. If the lowest system version you support is API level 11 or higher, then you can use a regular
Activity.Here is an example layout file that adds two fragments to an activity when the device screen is considered "large" (specified by the
largequalifier in the directory name).res/layout-large/news_articles.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.example.android.fragments.HeadlinesFragment" android:id="@+id/headlines_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.android.fragments.ArticleFragment" android:id="@+id/article_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>Tip: For more about creating layouts for different screen sizes, read Supporting Different Screen Sizes.Then apply the layout to your activity:
import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); } }If you're using the v7 appcompat library, your activity should instead extend
ActionBarActivity, which is a subclass of
FragmentActivity(for more information, read Adding the Action Bar).Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts, as shown in the next lesson.
Building a Flexible UI
When designing your application to support a wide range of screen sizes, you can reuse your fragments in different layout configurations to optimize the user experience based on the available screen space.For example, on a handset device it might be appropriate to display just one fragment at a time for a single-pane user interface. Conversely, you may want to set fragments side-by-side on a tablet which has a wider screen size to display more information to the user.Figure 1. Two fragments, displayed in different configurations for the same activity on different screen sizes. On a large screen, both fragments fit side by side, but on a handset device, only one fragment fits at a time so the fragments must replace each other as the user navigates.The
FragmentManagerclass provides methods that allow you to add, remove, and replace fragments to an activity at runtime in order to create a dynamic experience.
相关文章推荐
- eth0 在系统启动时自动激活(z'z)
- eth0 在系统启动时自动激活
- eth0 在系统启动时自动激活
- linux系统启动自动激活网卡的解决方法
- linux系统启动自动激活网卡的解决方法
- Linux如何在系统启动时自动加载模块
- 系统开机自动运行程序和自动启动服务
- 实现vmare虚拟机系统随主机开机自动启动
- Linux如何在系统启动时自动加载内核模块
- Beaglebone Black——系统启动后BBB板子的盘符没有出来、系统启动自动挂载分区
- linux系统下设置oracle开机自动启动
- 系统自动启动程序之十大藏身之所
- centos7用shell创建守护进程,并设置为系统服务,开机自动启动
- SQL server 2000 在Windows 2003系统下 SQL SERVER AGENT自动启动服务。
- 小马激活工具激活系统后,电脑不能启动,出现错误 a disk read error occurred
- Oracle 11g数据库随系统自动启动与关闭的设置方法
- 使Upstart机制下的服务在系统启动时自动启动失效的三种方法+1
- CentOS6.X开机自动激活网卡(eth0)
- SQL server 2000 Agent 不能随系统 自动启动
- 绿色版Tomcat 启动 + 停止 + 随系统自动启动