Ⅰ android中fragment中 怎麼切換豎屏
你參考下,要回答這個問題,我們要先討論一些開發者在Activity生命周期中使用長時間後台任務時遇到的共同困難。然後,我們將介紹常見的兩種能解決問題但有不好的方法。最後,我們會用一個示例代碼說明推薦的解決方案,它用retained fragment來達到我們的目標。
配置改變&後台線程(Configuration Changes & Background Tasks)
配置發生變化以及銷毀和重新創建穿越了整個Activity的生命周期,並且引出一個問題,那就是這些事件的發生是不可預測並且在任何時候都可能觸發。並發的後台線程只加劇了這個問題。假設在Activity中啟動了一個AsyncTask,然後用戶馬上旋轉屏幕,這會導致Activity被銷毀和重新創建。當AsyncTask最後完成它的任務,它會將結果反饋到舊的Activity實例,完全沒有意識到新的activity已經被創建了。似乎這不是一個問題,新的Activity實例又會讓浪費寶貴的資源重新啟動一個後台線程,而不知道舊的AsyncTask已經在運行。由於這些原因,在配置變化的時候我們需要正確、有效地保存在Activity實例的活動對象。
不好的實踐:保存整個Activity
可能最有效和最常被濫用的解決方法是通過在Android manifest中設置android:configChanges屬性禁止默認的銷毀和重新創建行為。這個簡單的方法使得它對開發者很有吸引力;然而Google的工程師建議不這么做。主要的擔憂是:配置後,需要你在代碼中手動處理設備的配置變化。處理配置變化需要你採取很多額外的處理,以確保每一個字元串、布局、繪圖、尺寸等與當前設備的配置一致。如果你不小心,那麼你的應用程序可能會有一系列與資源定製方面有關的Bug。
另一個Google不鼓勵使用它的原因是許多開發者錯誤地認為,設置android:configChanges = "orientation"(這只是舉例說明),會神奇地避免他們的Activity在不可預知的場景中被銷毀和重新創建。其實不是這樣的。有多種原因可能導致配置發生變化,而不單單是屏幕橫豎屏的變化。將你的手機中的內容顯示在顯示器上,更改默認語言,修改設備默認的字體縮放,這三個簡單的例子都有可能觸發設備的配置變化。這些事件會向系統發出信號,銷毀並重建所有正在運行的Activity,在它們下一次resume的時候。所以設置android:configChanges屬性一般不是好的做法。
已經被棄用的方法:重寫()
在Honeycomb發布前,跨越Activity實例傳遞活動對象的推薦方法是重寫()和()方法。使用這種方法,傳遞跨越Activity 實例的活動對象僅僅需要在()將活動對象返回,然後在()中取出。截止API 13,這些方法都已經被棄用,以支持更有效的Fragment的setRetainInstance(boolean)方法。它提供了一個更簡潔,更模塊化的方式在配置變化的時候保存對象。我們將在下一節討論以Fragment為基礎的方法。
推薦的方法:在Retained Fragment中管理對象
自從Android3.0推出Fragment。跨越Activity保留活動對象的推薦方法是在一個Retained Fragment中包裝和管理它們。默認情況下,但配置發生變化時,Fragment會隨著它們的宿主Activity被創建和銷毀。調用Fragment#setRetaininstance(true)允許我們跳過銷毀和重新創建的周期。指示系統保留當前的fragment實例,即使是在Activity被創新創建的時候。不難想到使用fragment持有像運行中的線程、AsyncTask、Socket等對象將有效地解決上面的問題。
Ⅱ 關於Android中fragment的管理
底部使用fragment構建,用fragment元素組成一個集合,接下來使用activity把每個元素都添加進去.調用fragmentPagerAdapter作為適配器排版,然後調用viewpager顯示界面,底下菜單使用button點擊事件觸發,,同時實現滑動與點擊控制界面的效果
Ⅲ android 怎樣讓fragment全屏,隱藏標題欄
讓Fragment
所屬的Activity
全屏就可以了
Fragment
本身就是在Activity裡面存在的
後者是前者的容器容器全屏了
他自然也全屏了
一勞永逸的方法
在Manifest文件裡面的Aplication
裡面的那個theme
android:theme="@android:style/Theme.Black.NoTitleBar"
例如
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar"
>
Ⅳ android fragment多窗口怎麼使用
、Fragment的產生與介紹
關於fragment的實例,請參考android學習手冊,android學習手冊包含9個章節,108個例子,源碼文檔隨便看,例子都是可交互,可運行,
源碼採用android studio目錄結構,高亮顯示代碼,文檔都採用文檔結構圖顯示,可以快速定位。360手機助手中下載,圖標上有貝殼。
Android運行在各種各樣的設備中,有小屏幕的手機,超大屏的平板甚至電視。針對屏幕尺寸的差距,很多情況下,都是先針對手機開發一套App,然後拷貝一份,修改布局以適應平板神馬超級大屏的。難道無法做到一個App可以同時適應手機和平板么,當然了,必須有啊。Fragment的出現就是為了解決這樣的問題。你可以把Fragment當成Activity的一個界面的一個組成部分,甚至Activity的界面可以完全有不同的Fragment組成,更帥氣的是Fragment擁有自己的生命周期和接收、處理用戶的事件,這樣就不必在Activity寫一堆控制項的事件處理的代碼了。更為重要的是,你可以動態的添加、替換和移除某個Fragment。
2、Fragment的生命周期
Fragment必須是依存與Activity而存在的,因此Activity的生命周期會直接影響到Fragment的生命周期。官網這張圖很好的說明了兩者生命周期的關系:
可以看到Fragment比Activity多了幾個額外的生命周期回調方法:
onAttach(Activity)
當Fragment與Activity發生關聯時調用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
創建該Fragment的視圖
onActivityCreated(Bundle)
當Activity的onCreate方法返回時調用
onDestoryView()
與onCreateView想對應,當該Fragment的視圖被移除時調用
onDetach()
與onAttach相對應,當Fragment與Activity關聯被取消時調用
注意:除了onCreateView,其他的所有方法如果你重寫了,必須調用父類對於該方法的實現,
3、靜態的使用Fragment
嘿嘿,終於到使用的時刻了~~
這是使用Fragment最簡單的一種方式,把Fragment當成普通的控制項,直接寫在Activity的布局文件中。步驟:
1、繼承Fragment,重寫onCreateView決定Fragemnt的布局
2、在Activity中聲明此Fragment,就當和普通的View一樣
下面展示一個例子(我使用2個Fragment作為Activity的布局,一個Fragment用於標題布局,一個Fragment用於內容布局):
TitleFragment的布局文件:
[html] view plain print?
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/title_bar">
<ImageButton
android:id="@+id/id_title_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@drawable/showleft_selector"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="我不是微信"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/title_bar" >
<ImageButton
android:id="@+id/id_title_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@drawable/showleft_selector" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="我不是微信"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
TitleFragment
[java] view plain print?
packagecom.zhy.zhy_fragments;
importandroid.app.Fragment;
importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.ViewGroup;
importandroid.widget.ImageButton;
importandroid.widget.Toast;
{
privateImageButtonmLeftMenu;
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
BundlesavedInstanceState)
{
Viewview=inflater.inflate(R.layout.fragment_title,container,false);
mLeftMenu=(ImageButton)view.findViewById(R.id.id_title_left_btn);
mLeftMenu.setOnClickListener(newOnClickListener()
{
@Override
publicvoidonClick(Viewv)
{
Toast.makeText(getActivity(),
"!",
Toast.LENGTH_SHORT).show();
}
});
returnview;
}
}
package com.zhy.zhy_fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;
public class TitleFragment extends Fragment
{
private ImageButton mLeftMenu;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_title, container, false);
mLeftMenu = (ImageButton) view.findViewById(R.id.id_title_left_btn);
mLeftMenu.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getActivity(),
"i am an ImageButton in TitleFragment ! ",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
同理還有ContentFragment的其布局文件:
[html] view plain print?
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="使用Fragment做主面板"
android:textSize="20sp"
android:textStyle="bold"/>
</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="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="使用Fragment做主面板"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
[java] view plain print?
packagecom.zhy.zhy_fragments;
importandroid.app.Fragment;
importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
{
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
BundlesavedInstanceState)
{
returninflater.inflate(R.layout.fragment_content,container,false);
}
}
package com.zhy.zhy_fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ContentFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_content, container, false);
}
}
MainActivity
[java] view plain print?
packagecom.zhy.zhy_fragments;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.Window;
{
@Override
protectedvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}
package com.zhy.zhy_fragments;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}
Activity的布局文件:
[java] view plain print?
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/id_fragment_title"
android:name="com.zhy.zhy_fragments.TitleFragment"
android:layout_width="fill_parent"
android:layout_height="45dp"/>
<fragment
android:layout_below="@id/id_fragment_title"
android:id="@+id/id_fragment_content"
android:name="com.zhy.zhy_fragments.ContentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
Ⅳ Android fragment 中控制某一個界面全屏與否
展開全部
這是我寫的博客裡面的代碼,可以參考如下,設置全屏與否:
/**
*
添加橫豎屏監聽,隱藏圖形的菜單控制項
*/
@Override
public
void
onConfigurationChanged(Configuration
newConfig)
{
super.onConfigurationChanged(newConfig);
if
(newConfig.orientation
==
Configuration.ORIENTATION_LANDSCAPE)
{
//
//
設置全屏
//
//
設置全屏
getActivity().getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else
if
(newConfig.orientation
==
Configuration.ORIENTATION_PORTRAIT)
{
WindowManager.LayoutParams
attrs
=
getActivity().getWindow()
.getAttributes();
attrs.flags
&=
~WindowManager.LayoutParams.FLAG_FULLSCREEN;
getActivity().getWindow().setAttributes(attrs);
}
}
Ⅵ android中fragment內容顯示問題
樓主,fragment布局文件粘上來給我看下。
Ⅶ 關於Android Fragment的問題
1、概述
首先我們簡單回顧一下,相信大家對Fragment的都不陌生,對於Fragment的使用,一方面Activity需要在布局中為Fragment安排位置,另一方面需要管理好Fragment的生命周期。Activity中有個FragmentManager,其內部維護fragment隊列,以及fragment事務的回退棧。
一般情況下,我們在Activity裡面會這么添加Fragment:
[java] view plain 在CODE上查看代碼片派生到我的代碼片
public class MainActivity extends FragmentActivity
{
private ContentFragment mContentFragment ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
mContentFragment = (ContentFragment) fm.findFragmentById(R.id.id_fragment_container);
if(mContentFragment == null )
{
mContentFragment = new ContentFragment();
fm.beginTransaction().add(R.id.id_fragment_container,mContentFragment).commit();
}
}
}
針對上面代碼,問兩個問題:
1、為什麼需要判null呢?
主要是因為,當Activity因為配置發生改變(屏幕旋轉)或者內存不足被系統殺死,造成重新創建時,我們的fragment會被保存下來,但是會創建新的FragmentManager,新的FragmentManager會首先會去獲取保存下來的fragment隊列,重建fragment隊列,從而恢復之前的狀態。
2、add(R.id.id_fragment_container,mContentFragment)中的布局的id有何作用?
一方面呢,是告知FragmentManager,此fragment的位置;另一方面是此fragment的唯一標識;就像我們上面通過fm.findFragmentById(R.id.id_fragment_container)查找~~
好了,簡單回顧了一下基本用法,具體的還請參考上面的博客或者其他資料,接下來,介紹一些使用的意見~~
2、Fragment Arguments
下面描述一個簡單的場景,比如我們某個按鈕觸發Activity跳轉,需要通過Intent傳遞參數到目標Activity的Fragment中,那麼此Fragment如何獲取當前的Intent的值呢?
有哥們會說,這個簡單?看我的代碼(問題代碼):
[java] view plain 在CODE上查看代碼片派生到我的代碼片
public class ContentFragment extends Fragment
{
private String mArgument ;
public static final String ARGUMENT ="argument";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mArgument = getActivity().getIntent().getStringExtra(ARGUMENT);
}
我們直接在Fragment的onCreate中,拿到宿主Activty,宿主Activity中肯定能通過getIntent拿到Intent,然後通過get方法,隨意拿參數~~
這么寫,功能上是實現了,但是呢?存在一個大問題:我們用Fragment的一個很大的原因,就是為了復用。你這么寫,相當於這個Fragment已經完全和當前這個宿主Activity綁定了,復用直接廢了~~~所以呢?我們換種方式,推薦使用arguments來創建Fragment。
[java] view plain 在CODE上查看代碼片派生到我的代碼片
public class ContentFragment extends Fragment
{
private String mArgument;
public static final String ARGUMENT = "argument";
@Override
public void onCreate(Bun www.hbbz08.com dle savedInstanceState)
{
super.onCreate(savedInstanceState);
// mArgument = getActivity().getIntent().getStringExtra(ARGUMENT);
Bundle bundle = getArguments();
if (bundle != null)
mArgument = bundle.getString(ARGUMENT);
}
/**
* 傳入需要的參數,設置給arguments
* @param argument
* @return
*/
public static ContentFragment newInstance(String argument)
{
Bundle bundle = new Bundle();
bundle.putString(ARGUMENT, argument);
ContentFragment contentFragment = new ContentFragment();
contentFragment.setArguments(bundle);
return contentFragment;
}
給Fragment添加newInstance方法,將需要的參數傳入,設置到bundle中,然後setArguments(bundle),最後在onCreate中進行獲取;
這樣就完成了Fragment和Activity間的解耦。當然了這里需要注意:
setArguments方法必須在fragment創建以後,添加給Activity前完成。千萬不要,首先調用了add,然後設置arguments。
Ⅷ dialog怎麼全屏 怎麼在Fragment里來顯
這個問題很讓人糾結,因為我要在popopwindows裡面使用fragment,但是在popopwindows裡面找不到它的父控制項,於是轉戰使用DialogFragment,但是讓它全屏是一件很頭疼的事情,找了好久總於找到,分享給需要的朋友
記著要在DialogFragment的oncreate裡面做
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
Ⅸ android中在多個Fragment中切換是否沉浸,界面布局不能鋪滿或者被拉伸
天想試試沉浸欄的使用,但是按照網上相應的方法設置完成後,沒有達到想要的結果。使用情況是activity配合groupradio實現fragment切換,每個fragment的狀態欄需要顯示不同的顏色。通過hide和show的方式控制顯示當前fragment。在對應的xml中設置Android:fitsSystemWindows="true",以達到改變狀態欄顏色的問題(具體如何配置請查閱其他文檔)。
但是這樣做第一個fragment達到了想要的效果,其他三個狀態欄的顏色正確,但是padding沒有設置成功。標題欄佔用狀態欄的位置。
解決方法:
將xml中的android:fitsSystemWindows="true"去掉,通過代碼的方式來控制。在默認第一個fragment的onCreateView中設置view.setFitsSystemWindows(true);該view為該fragment對應的view。在onHiddenChanged中,設置對應的狀態。
詳細