"Turn" Pro Android Learning Note (40): Fragment (5): adapts to different screens or typography

Source: Internet
Author: User

Directory (?) [-]

    1. Set different typography styles for horizontal and vertical rows
    2. Rewrite code

For fragment, it often involves different screen sizes and different typography styles. We make a change in the basic small example, in the horizontal, is still realistic about two fragment, in the vertical row, as shown:

Only one fragment is displayed on the screen, click on the number on the list to enter the activity of the introduction. Here's how it's implemented.

Set different typography styles for horizontal and vertical rows

In the Pro Android learning Note (iv): Learn about resource and configuration changes in Android resources (below), we show you how to set different resources with the resource folder name. The default Layout/fragment_basic.xml, which is set to a vertical format, contains only 1 fragment, as follows

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Horizontal" >
<fragment class= "Com.wei.flowingflying.pro.ProFragment.TitleFragment"
Android:id= "@+id/titles"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"/>
</LinearLayout>

For layout-land/fragment_basic.xml, the horizontal format, the original small example XML file, contains 2 fragment, which is no longer duplicated here.

Rewrite code

In activity, Titlefragment is automatically called by Setcontentview (), based on the class attribute of fragment in XML, and in Titlefragment by calling Acitivity ShowDetail (int index), to show the book's introduction in detail.

private int mcurcheckposition =-1;

The original set 0, which is now-1, the purpose is to distinguish whether the user interactive selection book directory, or the horizontal screen is the default display of the bibliography of the serial number.

For fragmentbasictest, some modifications are also made, as follows:

public class Fragmentbasictest extends activity{
@Override
protected void OnCreate (Bundle savedinstancestate) {
Setcontentview (R.layout.fragment_basic);
}
Determine if you need to display multiple fragment
Private Boolean Ismultpane () {
Return Getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape;
}

public void showDetails (int index) {
if (Ismultpane ()) {//show Left and right two fragement, the code does not change
if (Index < 0)
index = 0;
Detailfragment detail = (detailfragment) Getfragmentmanager (). Findfragmentbyid (R.id.details);
if (detail = = null) {
Addfirstfragment (index);
}else if (detail.getshowindex () = index) {
Addfragmenttostack (index);
}
}else{//Displays only one fragment, evoking activity that displays the intro content
if (Index < 0)
Return
Intent Intent = new Intent ();
Intent.setclass (this, detailactivity.class);
Intent.putextra ("index", index);
StartActivity (Intent);
}
}
......
}

Detailactivity can use XML to set a specific layout, to the normal activity, the content of the book is presented and the user. However, we already have the relevant display processing detailfragment, we should make full use of to ensure the consistency of the code.

public class Detailactivity extends activity{
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);

When the user switches from portrait mode to horizontal screen, detects that the mode is horizontal, exits the activity, and withdraws to the previous activity, showing the double fragment of the horizontal screen. Otherwise we will only see this activity turn.
if (Getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape) {
Finish ();
Return
}

Intent Intent = Getintent ();
if (intent = = null) {
Finish ();
Return
}
//Add detailfragment to Acitvity
Detailfragment detail = detailfragment.newinstance (Intent.getextras ());
Getfragmentmanager (). BeginTransaction ()
. Add (Android. R.id.content, detail)Android. The r.id.content is the top-level view container of the activity, and the fragment is associated with it.
. commit ();
}

}

In Fragmenttransaction's Add (int containerviewid, Fragment Fragment), the container ID uses android.r.id.content, which will get the root element of the view (root element of a view).

This post covers the example code that can be downloaded in Pro Android Learning: Fragment.

RELATED Links: My Android development related articles

"Turn" Pro Android Learning Note (40): Fragment (5): adapts to different screens or typography

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.