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 = {
"線性布局是將放入其中的組件按照垂直或水平方向來(lái)布局,也就是控制放入其中的組件橫向排列或縱向排列。"
+ "在線性布局中,每一行(針對(duì)垂直排列)或每一列(針對(duì)水平排列)中只能放一個(gè)組件。"
+ "并且Android的線性布局不會(huì)換行,當(dāng)組件一個(gè)挨著一個(gè)排列到窗體的邊緣后,剩下的組件將不會(huì)被顯示出來(lái)。",
"表格布局與常見(jiàn)的表格類似,它以行、列的形式來(lái)管理放入其中的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)開(kāi)始布局,"
+ "多個(gè)組件層疊排序,后面的組件覆蓋前面的組件。",
"相對(duì)布局是指按照組件之間的相對(duì)位置來(lái)進(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)容
}
}
// 重寫(xiě)onSaveInstanceState()方法,保存當(dāng)前選中的列表項(xiàng)的索引值
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", curCheckPosition);
}
// 重寫(xiě)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); // 替換原來(lái)顯示的詳細(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); // 開(kāi)啟一個(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ī)界面中,通過(guò)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é)
以上就是這篇文章的全部?jī)?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開(kāi)源框架的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)電子羅盤(pán)(指南針)方向傳感器的應(yīng)用
- Android開(kāi)發(fā)實(shí)現(xiàn)ListView點(diǎn)擊展開(kāi)收起效果示例
- Android中BroadcastReceiver案例講解
- Android中Gallery和ImageSwitcher的使用實(shí)例
相關(guān)文章
android studio 清單配置文件androidmainfest.xml詳細(xì)解讀
AndroidManifest官方解釋是應(yīng)用清單,每個(gè)應(yīng)用的根目錄中都必須包含一個(gè),并且文件名必須一模一樣,這個(gè)文件中包含了APP的配置信息,系統(tǒng)需要根據(jù)里面的內(nèi)容運(yùn)行APP的代碼,顯示界面,這篇文章介紹了android studio 清單配置文件androidmainfest.xml解讀,需要的朋友可以參考下2024-04-04
Android中的Retrofit+OkHttp+RxJava緩存架構(gòu)使用
Retrofit和OkHttp API以及JVM擴(kuò)展RxJava都是開(kāi)源項(xiàng)目,大家可以輕松在GitHub上找到,下載和基本配置部分這里我們不作重點(diǎn),主要還是來(lái)看一下Android中的Retrofit+OkHttp+RxJava緩存架構(gòu)使用:2016-06-06
Flutter 插件url_launcher簡(jiǎn)介
最近項(xiàng)目需求是打開(kāi)一個(gè)連接跳轉(zhuǎn)到安卓或蘋(píng)果默認(rèn)的瀏覽器。雖然開(kāi)始一個(gè)簡(jiǎn)單的要求,其中的一個(gè)細(xì)節(jié)就是執(zhí)行打開(kāi)網(wǎng)頁(yè)這一操作后,不能看上去像在應(yīng)用內(nèi)部打開(kāi),看上去要在應(yīng)用外部打開(kāi),今天小編給大家介紹Flutter 插件url_launcher的相關(guān)知識(shí),感興趣的朋友一起看看吧2020-04-04
Android開(kāi)發(fā)歡迎頁(yè)點(diǎn)擊跳過(guò)倒計(jì)時(shí)進(jìn)入主頁(yè)
沒(méi)點(diǎn)擊跳過(guò)自然進(jìn)入主頁(yè),點(diǎn)擊跳過(guò)之后立即進(jìn)入主頁(yè),這個(gè)功能怎么實(shí)現(xiàn)呢,本文通過(guò)實(shí)例代碼給大家介紹Android開(kāi)發(fā)歡迎頁(yè)點(diǎn)擊跳過(guò)倒計(jì)時(shí)進(jìn)入主頁(yè),感興趣的朋友一起看看吧2023-12-12
Android Studio EditText點(diǎn)擊圖標(biāo)清除文本內(nèi)容的實(shí)例解析
這篇文章主要介紹了Android Studio EditText點(diǎn)擊圖標(biāo)清除文本內(nèi)容的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
Android studio 4.1打包失敗和插件錯(cuò)誤提示的解決
這篇文章主要介紹了Android studio 4.1打包失敗和插件錯(cuò)誤提示的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Flutter開(kāi)發(fā)之Shortcuts快捷鍵組件的用法詳解
在桌面端的開(kāi)發(fā)中,鍵盤(pán)快捷鍵是非常常見(jiàn)而必要的,F(xiàn)lutter?既然可以開(kāi)發(fā)桌面端應(yīng)用,那必然要提供自定義快捷鍵,所以本文就來(lái)和大家講講Shortcuts組件的簡(jiǎn)單使用吧2023-05-05

