您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)怎么在Android中使用Fragment實(shí)現(xiàn)分屏顯示處理橫豎屏顯示,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
布局文件如下:
可以看出有兩個(gè)資源文件,一個(gè)是處理橫屏一個(gè)是豎屏
第一個(gè):
<?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>
第二個(gè):
<?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 { // 標(biāo)題 public static final String[] TITLES = { "線性布局", "表格布局", "幀布局", "相對布局" }; // 詳細(xì)內(nèi)容 public static final String[] DETAIL = { "線性布局是將放入其中的組件按照垂直或水平方向來布局,也就是控制放入其中的組件橫向排列或縱向排列。" + "在線性布局中,每一行(針對垂直排列)或每一列(針對水平排列)中只能放一個(gè)組件。" + "并且Android的線性布局不會(huì)換行,當(dāng)組件一個(gè)挨著一個(gè)排列到窗體的邊緣后,剩下的組件將不會(huì)被顯示出來。", "表格布局與常見的表格類似,它以行、列的形式來管理放入其中的UI組件。" + "表格布局使用<TableLayout>標(biāo)記定義,在表格布局中,可以添加多個(gè)<TableRow>標(biāo)記," + "每個(gè)<TableRow>標(biāo)記占用一行,由于<TableRow>標(biāo)記也是容器,所以在該標(biāo)記中還可添加其他組件," + "在<TableRow>標(biāo)記中,每添加一個(gè)組件,表格就會(huì)增加一列。在表格布局中,列可以被隱藏," + "也可以被設(shè)置為伸展的,從而填充可利用的屏幕空間,也可以設(shè)置為強(qiáng)制收縮,直到表格匹配屏幕大小。", "在幀布局管理器中,每加入一個(gè)組件,都將創(chuàng)建一個(gè)空白的區(qū)域,通常稱為一幀," + "這些幀都會(huì)根據(jù)gravity屬性執(zhí)行自動(dòng)對齊。默認(rèn)情況下,幀布局是從屏幕的左上角(0,0)坐標(biāo)點(diǎn)開始布局," + "多個(gè)組件層疊排序,后面的組件覆蓋前面的組件。", "相對布局是指按照組件之間的相對位置來進(jìn)行布局,如某個(gè)組件在另一個(gè)組件的左邊、右邊、上面或下面等。" }; }
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 { // 創(chuàng)建一個(gè)DetailFragment的新實(shí)例,其中包括要傳遞的數(shù)據(jù)包 public static DetailFragment newInstance(int index) { DetailFragment f = new DetailFragment(); // 將index作為一個(gè)參數(shù)傳遞 Bundle bundle = new Bundle(); // 實(shí)例化一個(gè)Bundle對象 bundle.putInt("index", index); // 將索引值添加到Bundle對象中 f.setArguments(bundle); // 將bundle對象作為Fragment的參數(shù)保存 return f; } public int getShownIndex() { return getArguments().getInt("index", 0); // 獲取要顯示的列表項(xiàng)索引 } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { return null; } ScrollView scroller = new ScrollView(getActivity()); // 創(chuàng)建一個(gè)滾動(dòng)視圖 TextView text = new TextView(getActivity()); // 創(chuàng)建一個(gè)文本框?qū)ο? text.setPadding(10, 10, 10, 10); // 設(shè)置內(nèi)邊距 scroller.addView(text); // 將文本框?qū)ο筇砑拥綕L動(dòng)視圖中 text.setText(Data.DETAIL[getShownIndex()]); // 設(shè)置文本框中要顯示的文本 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; // 是否在一屏上同時(shí)顯示列表和詳細(xì)內(nèi)容 int curCheckPosition = 0; // 當(dāng)前選擇的索引位置 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_checked, Data.TITLES)); // 為列表設(shè)置適配器 View detailFrame = getActivity().findViewById(R.id.detail); // 獲取布局文件中添加的FrameLayout幀布局管理器 dualPane = detailFrame != null && detailFrame.getVisibility() == View.VISIBLE; // 判斷是否在一屏上同時(shí)顯示列表和詳細(xì)內(nèi)容 if (savedInstanceState != null) { curCheckPosition = savedInstanceState.getInt("curChoice", 0); // 更新當(dāng)前選擇的索引位置 } if (dualPane) { // 如果在一屏上同時(shí)顯示列表和詳細(xì)內(nèi)容 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 設(shè)置列表為單選模式 showDetails(curCheckPosition); // 顯示詳細(xì)內(nèi)容 } } // 重寫onSaveInstanceState()方法,保存當(dāng)前選中的列表項(xiàng)的索引值 @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); // 調(diào)用showDetails()方法顯示詳細(xì)內(nèi)容 } void showDetails(int index) { curCheckPosition = index; // 更新保存當(dāng)前索引位置的變量的值為當(dāng)前選中值 if (dualPane) { // 當(dāng)在一屏上同時(shí)顯示列表和詳細(xì)內(nèi)容時(shí) getListView().setItemChecked(index, true); // 設(shè)置選中列表項(xiàng)為選中狀態(tài) DetailFragment details = (DetailFragment) getFragmentManager() .findFragmentById(R.id.detail); // 獲取用于顯示詳細(xì)內(nèi)容的Fragment if (details == null || details.getShownIndex() != index) { // 如果如果 details = DetailFragment.newInstance(index); // 創(chuàng)建一個(gè)新的DetailFragment實(shí)例用于顯示當(dāng)前選擇項(xiàng)對應(yīng)的詳細(xì)內(nèi)容 // 要在activity中管理fragment, 需要使用FragmentManager FragmentTransaction ft = getFragmentManager() .beginTransaction();// 獲得一個(gè)FragmentTransaction的實(shí)例 ft.replace(R.id.detail, details); // 替換原來顯示的詳細(xì)內(nèi)容 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); // 設(shè)置轉(zhuǎn)換效果 ft.commit(); // 提交事務(wù) } } else { // 在一屏上只能顯示列表或詳細(xì)內(nèi)容中的一個(gè)內(nèi)容時(shí) // 使用一個(gè)新的Activity顯示詳細(xì)內(nèi)容 Intent intent = new Intent(getActivity(), MainActivity.DetailActivity.class); // 創(chuàng)建一個(gè)Intent對象 intent.putExtra("index", index); // 設(shè)置一個(gè)要傳遞的參數(shù) startActivity(intent); // 開啟一個(gè)指定的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); } // 創(chuàng)建一個(gè)繼承Activity的內(nèi)部類,用于在手機(jī)界面中,通過Activity顯示詳細(xì)內(nèi)容 public static class DetailActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 判斷是否為橫屏,如果為橫屏,則結(jié)束當(dāng)前Activity,準(zhǔn)備使用Fragment顯示詳細(xì)內(nèi)容 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { finish(); // 結(jié)束當(dāng)前Activity return; } if (savedInstanceState == null) { // // 在初始化時(shí)插入一個(gè)顯示詳細(xì)內(nèi)容的Fragment DetailFragment details = new DetailFragment();// 實(shí)例化DetailFragment的對象 details.setArguments(getIntent().getExtras()); // 設(shè)置要傳遞的參數(shù) getFragmentManager().beginTransaction() .add(android.R.id.content, details).commit(); // 添加一個(gè)顯示詳細(xì)內(nèi)容的Fragment } } } }
以上就是怎么在Android中使用Fragment實(shí)現(xiàn)分屏顯示處理橫豎屏顯示,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。