Android中Fragment的分屏顯示處理橫豎屏顯示

來源:互聯網
上載者:User

標籤:fragment   橫豎屏顯示   分屏顯示   

示範效果如下:


另外在豎屏的時候是這樣的效果:

布局檔案如下:


可以看出有兩個資源檔,一個是處理橫屏一個是豎屏

第一個:

<?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        android:id="@+id/titles"        android:layout_width="0px"        android:layout_height="match_parent"        android:layout_weight="1"        class="com.xuliugen.frag.ListFragment" /></LinearLayout>

第二個:

<?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        android:id="@+id/titles"        android:layout_width="0px"        android:layout_height="match_parent"        android:layout_weight="1"        class="com.xuliugen.frag.ListFragment" />    <FrameLayout        android:id="@+id/detail"        android:layout_width="0px"        android:layout_height="match_parent"        android:layout_weight="2"        android:background="?android:attr/detailsElementBackground" /></LinearLayout>
類代碼


Data.java

public final class Data {    // 標題    public static final String[] TITLES = { "線性布局", "表格版面配置", "幀布局", "相對布局"    };    // 詳細內容    public static final String[] DETAIL = {            "線性布局是將放入其中的組件按照垂直或水平方向來布局,也就是控制放入其中的組件橫向排列或縱向排列。"                    + "線上性布局中,每一行(針對垂直排列)或每一列(針對水平排列)中只能放一個組件。"                    + "並且Android的線性布局不會換行,當組件一個挨著一個排列到表單的邊緣後,剩下的組件將不會被顯示出來。",            "表格版面配置與常見的表格類似,它以行、列的形式來管理放入其中的UI組件。"                    + "表格版面配置使用<TableLayout>標記定義,在表格版面配置中,可以添加多個<TableRow>標記,"                    + "每個<TableRow>標記佔用一行,由於<TableRow>標記也是容器,所以在該標記中還可添加其他組件,"                    + "在<TableRow>標記中,每添加一個組件,表格就會增加一列。在表格版面配置中,列可以被隱藏,"                    + "也可以被設定為伸展的,從而填充可利用的螢幕空間,也可以設定為強制收縮,直到表格匹配螢幕大小。",            "在幀布局管理器中,每加入一個組件,都將建立一個空白的地區,通常稱為一幀,"                    + "這些幀都會根據gravity屬性執行自動對齊。預設情況下,幀布局是從螢幕的左上方(0,0)座標點開始布局,"                    + "多個組件層疊排序,後面的組件覆蓋前面的組件。",            "相對布局是指按照組件之間的相對位置來進行布局,如某個組件在另一個組件的左邊、右邊、上面或下面等。" };}

DetailFragment.java

package com.xuliugen.frag;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ScrollView;import android.widget.TextView;public class DetailFragment extends Fragment {    // 建立一個DetailFragment的新執行個體,其中包括要傳遞的資料包    public static DetailFragment newInstance(int index) {        DetailFragment f = new DetailFragment();        // 將index作為一個參數傳遞        Bundle bundle = new Bundle(); // 執行個體化一個Bundle對象        bundle.putInt("index", index); // 將索引值添加到Bundle對象中        f.setArguments(bundle); // 將bundle對象作為Fragment的參數儲存        return f;    }    public int getShownIndex() {        return getArguments().getInt("index", 0); // 擷取要顯示的清單項目索引    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        if (container == null) {            return null;        }        ScrollView scroller = new ScrollView(getActivity()); // 建立一個滾動視圖        TextView text = new TextView(getActivity()); // 建立一個文字框對象        text.setPadding(10, 10, 10, 10); // 設定內邊距        scroller.addView(text); // 將文字框對象添加到滾動視圖中        text.setText(Data.DETAIL[getShownIndex()]); // 設定文字框中要顯示的文本        return scroller;    }}

ListFragment.java

package com.xuliugen.frag;import android.app.FragmentTransaction;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;public class ListFragment extends android.app.ListFragment {    boolean dualPane; // 是否在一屏上同時顯示列表和詳細內容    int curCheckPosition = 0; // 當前選擇的索引位置    @Override    public void onActivityCreated(Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        setListAdapter(new ArrayAdapter<String>(getActivity(),                android.R.layout.simple_list_item_checked, Data.TITLES)); // 為列表設定適配器        View detailFrame = getActivity().findViewById(R.id.detail); // 擷取布局檔案中添加的FrameLayout幀布局管理器        dualPane = detailFrame != null                && detailFrame.getVisibility() == View.VISIBLE; // 判斷是否在一屏上同時顯示列表和詳細內容        if (savedInstanceState != null) {            curCheckPosition = savedInstanceState.getInt("curChoice", 0); // 更新當前選擇的索引位置        }        if (dualPane) { // 如果在一屏上同時顯示列表和詳細內容            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 設定列表為單選模式            showDetails(curCheckPosition); // 顯示詳細內容        }    }    // 重寫onSaveInstanceState()方法,儲存當前選中的清單項目的索引值    @Override    public void onSaveInstanceState(Bundle outState) {        super.onSaveInstanceState(outState);        outState.putInt("curChoice", curCheckPosition);    }    // 重寫onListItemClick()方法    @Override    public void onListItemClick(ListView l, View v, int position, long id) {        showDetails(position); // 調用showDetails()方法顯示詳細內容    }    void showDetails(int index) {        curCheckPosition = index; // 更新儲存當前索引位置的變數的值為當前選中值        if (dualPane) { // 當在一屏上同時顯示列表和詳細內容時            getListView().setItemChecked(index, true); // 設定選中清單項目為選中狀態            DetailFragment details = (DetailFragment) getFragmentManager()                    .findFragmentById(R.id.detail); // 擷取用於顯示詳細內容的Fragment            if (details == null || details.getShownIndex() != index) { // 如果如果                details = DetailFragment.newInstance(index); // 建立一個新的DetailFragment執行個體用於顯示當前選擇項對應的詳細內容                // 要在activity中管理fragment, 需要使用FragmentManager                FragmentTransaction ft = getFragmentManager()                        .beginTransaction();// 獲得一個FragmentTransaction的執行個體                ft.replace(R.id.detail, details); // 替換原來顯示的詳細內容                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); // 設定轉換效果                ft.commit(); // 提交事務            }        } else { // 在一屏上只能顯示列表或詳細內容中的一個內容時            // 使用一個新的Activity顯示詳細內容            Intent intent = new Intent(getActivity(),                    MainActivity.DetailActivity.class); // 建立一個Intent對象            intent.putExtra("index", index); // 設定一個要傳遞的參數            startActivity(intent); // 開啟一個指定的Activity        }    }}

MainActivity.java

package com.xuliugen.frag;import android.app.Activity;import android.content.res.Configuration;import android.os.Bundle;public class MainActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }    // 建立一個繼承Activity的內部類,用於在手機介面中,通過Activity顯示詳細內容    public static class DetailActivity extends Activity {        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            // 判斷是否為橫屏,如果為橫屏,則結束當前Activity,準備使用Fragment顯示詳細內容            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {                finish(); // 結束當前Activity                return;            }            if (savedInstanceState == null) { //                // 在初始化時插入一個顯示詳細內容的Fragment                DetailFragment details = new DetailFragment();// 執行個體化DetailFragment的對象                details.setArguments(getIntent().getExtras()); // 設定要傳遞的參數                getFragmentManager().beginTransaction()                        .add(android.R.id.content, details).commit(); // 添加一個顯示詳細內容的Fragment            }        }    }}

Android中Fragment的分屏顯示處理橫豎屏顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.