Androidの断片化単一のフレーム
単一フレームの破片:単一フレームの断片は、ハンドヘルドデバイス(携帯電話)、アンドロイド3.0以降のサポートなどの小型スクリーンデバイス向けに設計されています。
例
この例では、独自の作品を作成する方法について説明します。 デバイスが別の鉛直スクリーン時間で使用する場合ここでは、デバイスで使用されるものである2つの断片を作成クロススクリーンです。 開始の手順に従ってみましょう。
ステップ | 説明 |
---|---|
1 | シングル断片という名前のAndroidアプリケーションを作成するには、Android StudioのIDEを使用して、パッケージ名cn.uprogrammer.singlefragments。 |
2 | MainActivity.java以下に示すように、メインアクティブなファイルを変更します。 ここでは、デバイスの向きを確認します。また、異なる部分との間で切り替えるには、このに基づいています。 |
3 | cn.uprogrammer.singlefragmentsパッケージおよび関連する方法でPortraitFragment.javaとLandscapeFragment.java 2つのファイルを作成します。 |
4 | 二つの断片のレイアウトを定義するレイアウトファイルのres /レイアウト/ landscape_fragment.xmlおよび解像度/レイアウト/ portrait_fragment.xmlを作成します。 |
5 | 二つの断片を含むように解像度/レイアウト/ activity_main.xmlを変更します。 |
6 | 内のres /値/のstrings.xmlに一定の必要性を定義します。 |
7 | アプリケーションを実行し、アプリケーションに加えられた変更の結果を確認するためにAndroidのエミュレータを起動します。 |
以下は、アクティブなファイルのsrc / cn.uprogrammer.singlefragments / MainActivity.javaの主な内容は以下のとおりです。
package cn.uprogrammer.singlefragment; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.res.Configuration; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Configuration config = getResources().getConfiguration(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); /** * 检测设备方向,并做相应地操作。 */ if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { /** * 设备的横屏模式。 */ LandscapeFragment ls_fragment = new LandscapeFragment(); fragmentTransaction.replace(android.R.id.content, ls_fragment); }else{ /** * 设备的竖屏模式。 */ PortraitFragment pm_fragment = new PortraitFragment(); fragmentTransaction.replace(android.R.id.content, pm_fragment); } fragmentTransaction.commit(); } }
パッケージcn.uprogrammer.singlefragmentsに2断片化したファイルLandscapeFragment.javaとPortraitFragment.javaを作成します。
以下は、コンテンツLandscapeFragment.javaファイルです。
package cn.uprogrammer.singlefragment; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class LandscapeFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * Inflate the layout for this fragment */ return inflater.inflate( R.layout.landscape_fragment, container, false); } }
以下は、コンテンツPortraitFragment.javaファイルです。
package cn.uprogrammer.singlefragment; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class PortraitFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * Inflate the layout for this fragment */ return inflater.inflate( R.layout.portrait_fragment, container, false); } }
ディレクトリのres /レイアウトディレクトリ内の2つのレイアウトファイルのlandscape_fragment.xmlとportrait_fragment.xmlを作成します。
以下は、コンテンツlandscape_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="28sp" /> <!-- More GUI components go here --> </LinearLayout>
以下は、コンテンツportrait_fragment.xmlファイルです。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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="28sp" /> <!-- More GUI components go here --> </LinearLayout>
以下は、2枚が含まれているのres /レイアウト/ activity_main.xmlファイルの内容です。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <fragment android:id="@+id/landscape_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:id="@+id/portrait_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
RES /値を確認/のstrings.xmlファイルには以下が含まれています。
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Single Fragment</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="landscape_message">这是横屏模式碎片</string> <string name="portrait_message">这是竖屏模式碎片</string> </resources>
ちょうど変更されたシングルフラグメントのアプリケーションを実行してみましょう。 私は、インストール環境中にAVDを作成したと仮定します。 アクティブなファイルでプロジェクトを開き、ツールバーのをクリック アンドロイドStudioでアプリケーションを実行するためのアイコン。 Androidのメーカーは、AVD上でアプリケーションをインストールし、それを開始します。 すべてがうまくいけば、次のようにエミュレータウィンドウに表示されます。
エミュレータ画面モードの方向を変更するには、以下によると:
- 肖像画やその逆のような風景の中には、fn +コントロール+ F11マック変更
- 窓にCTRL + F11
- Linux上でCTRL + F11
あなたはモードを変更すると、ページの実現に適した風景モードが表示されます。
この方法では、未使用のピースを使って、同じイベントで異なるインタフェースを達成することができます。 あなたは、インターフェイスコンポーネントの異なるタイプを使用して、ニーズに応じてインターフェイスを構築することができます。