Vous êtes sur la page 1sur 7

Fragment Example

Ctrl F11 switches the emulator between landscape and portrait

Activity Example with


Fragments

public class MainActivity extends Activity


{
@Override protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction =
fragmentManager.beginTransaction();
if
(config.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
transaction.replace(android.R.id.content, new Landscape());
}
else
{ transaction.replace(android.R.id.content, new Portrait()); }
transaction.commit();
}
}

Note: Android.R.id.content refers to the root element


of a view without having to know its actual ID value

Landscape and Portrait


Fragments
public class Landscape extends Fragment
{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
root, Bundle b)
{
return inflater.inflate( R.layout.lm_fragment, root, false);
}
}
public class Portrait extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
root, Bundle b)
{
return inflater.inflate( R.layout.pm_fragment, root, false);
} root: if Non-null, the view to which the fragment is attached
Inflate false: dont use root layout parameters
}

Fragment Views
File: res/layout/lm_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android =
"http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#7bae16">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/landscape_message"
android:textColor="#000000"
android:textSize="20sp" />
<!-- More GUI components -->
</LinearLayout>

File: res/layout/pm_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android =
"http://schemas.android.com/apk/res/androi
d"
android:orientation= "horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#666666">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/portrait_message"
android:textColor="#000000"
android:textSize="20sp" />
<!-- More GUI components -->
</LinearLayout>

res/layout/activity_main.
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="horizontal">
<fragment android:name="com.example.fragments"
android:id="@+id/lm_fragment" android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.fragments"
android:id="@+id/pm_fragment" android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>

res/values/strings.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyFragments</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="landscape_message">
This is Landscape mode fragment
</string>
<string name="portrait_message">
This is Portrait mode fragment
</string>
</resources>

Fragment & Menu

Vous aimerez peut-être aussi