Android中Fragment的分屏顯示處理橫豎屏顯示的實(shí)現(xiàn)方法
演示效果如下:
另外在豎屏的時(shí)候是這樣的效果:
布局文件如下:
可以看出有兩個(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 = { "線性布局", "表格布局", "幀布局", "相對(duì)布局" }; // 詳細(xì)內(nèi)容 public static final String[] DETAIL = { "線性布局是將放入其中的組件按照垂直或水平方向來布局,也就是控制放入其中的組件橫向排列或縱向排列。" + "在線性布局中,每一行(針對(duì)垂直排列)或每一列(針對(duì)水平排列)中只能放一個(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)對(duì)齊。默認(rèn)情況下,幀布局是從屏幕的左上角(0,0)坐標(biāo)點(diǎn)開始布局," + "多個(gè)組件層疊排序,后面的組件覆蓋前面的組件。", "相對(duì)布局是指按照組件之間的相對(duì)位置來進(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對(duì)象 bundle.putInt("index", index); // 將索引值添加到Bundle對(duì)象中 f.setArguments(bundle); // 將bundle對(duì)象作為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)對(duì)應(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對(duì)象 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的對(duì)象 details.setArguments(getIntent().getExtras()); // 設(shè)置要傳遞的參數(shù) getFragmentManager().beginTransaction() .add(android.R.id.content, details).commit(); // 添加一個(gè)顯示詳細(xì)內(nèi)容的Fragment } } } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Android手機(jī)屏幕px與dp互轉(zhuǎn)的工具類
- Android開源框架的SlidingFragment的使用示例
- Android實(shí)現(xiàn)手機(jī)定位的案例代碼
- Android側(cè)滑菜單之DrawerLayout用法詳解
- Android編程程序?qū)崿F(xiàn)一鍵鎖屏的方法講解
- Android實(shí)現(xiàn)手機(jī)震動(dòng)抖動(dòng)效果的方法
- Android實(shí)現(xiàn)電子羅盤(指南針)方向傳感器的應(yīng)用
- Android開發(fā)實(shí)現(xiàn)ListView點(diǎn)擊展開收起效果示例
- Android中BroadcastReceiver案例講解
- Android中Gallery和ImageSwitcher的使用實(shí)例
相關(guān)文章
Android Activity的跳轉(zhuǎn)與傳值詳解
這篇文章主要介紹了Android Activity的跳轉(zhuǎn)與傳值詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Android實(shí)現(xiàn)微信加號(hào)菜單模式
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)微信加號(hào)菜單模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08Android開發(fā)之RadioGroup的簡單使用與監(jiān)聽示例
這篇文章主要介紹了Android開發(fā)之RadioGroup的簡單使用與監(jiān)聽,結(jié)合實(shí)例形式分析了Android針對(duì)RadioGroup單選按鈕簡單實(shí)用技巧,需要的朋友可以參考下2017-07-07Android自定義RecyclerView Item頭部懸浮吸頂
這篇文章主要為大家詳細(xì)介紹了Android自定義RecyclerView Item頭部懸浮吸頂,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Android依據(jù)名字通過反射獲取在drawable中的圖片
依據(jù)圖片的名字,通過反射獲取其在drawable中的ID,在根據(jù)此ID顯示圖片,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06Android onActivityResult和setResult方法詳解及使用
這篇文章主要介紹了Android onActivityResult和setResult方法詳解及使用的相關(guān)資料,這里提供實(shí)例,幫助大家學(xué)習(xí)理解,需要的朋友可以參考下2016-12-12Android編程操作嵌入式關(guān)系型SQLite數(shù)據(jù)庫實(shí)例詳解
這篇文章主要介紹了Android編程操作嵌入式關(guān)系型SQLite數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android操作SQLite數(shù)據(jù)庫的基本技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01