Exemplo 12 usa fragmentos de acordo com a orientação da tela (landscape ou portrait) - PDF

Description
Exemplo 12 usa fragmentos de acordo com a orientação da tela (landscape ou portrait) 1)MainActivity.java package com.example.exemplo12; import android.app.activity; import android.app.fragmentmanager;

Please download to get full document.

View again

of 7
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information
Category:

Healthcare

Publish on:

Views: 21 | Pages: 7

Extension: PDF | Download: 0

Share
Transcript
Exemplo 12 usa fragmentos de acordo com a orientação da tela (landscape ou portrait) 1)MainActivity.java package com.example.exemplo12; import android.app.activity; import android.app.fragmentmanager; import android.app.fragmenttransaction; import android.content.res.configuration; public class MainActivity extends Activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); Configuration config = getresources().getconfiguration(); FragmentManager fragmentmanager = getfragmentmanager(); FragmentTransaction fragmenttransaction = fragmentmanager.begintransaction(); * Check the device orientation and act accordingly if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { * Landscape mode of the device LM_Fragment ls_fragment = new LM_Fragment(); fragmenttransaction.replace(android.r.id.content, ls_fragment); else{ * Portrait mode of the device PM_Fragment pm_fragment = new PM_Fragment(); fragmenttransaction.replace(android.r.id.content, pm_fragment); fragmenttransaction.commit(); 2) LM_Fragment.java package com.example.exemplo12; import android.app.fragment; public class LM_Fragment extends Fragment{ public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { * Inflate the layout for this fragment return inflater.inflate(r.layout.lm_fragment, container, false); 3) PM_Fragment.java package com.example.exemplo12; import android.app.fragment; public class PM_Fragment extends Fragment{ public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { * Inflate the layout for this fragment return inflater.inflate(r.layout.pm_fragment, container, false); 4) activity_main.xml xmlns:tools= http://schemas.android.com/tools android:baselinealigned= false android:layout_height= fill_parent android:orientation= horizontal android:name= com.example.fragments android:layout_weight= 1 android:layout_height= match_parent / android:name= com.example.fragments android:layout_weight= 2 android:layout_height= match_parent / 5) lm_fragment.xml LinearLayout xmlns:android= http://schemas.android.com/apk/res/android android:orientation= vertical android:layout_height= fill_parent android:background= #7bae16 android:textcolor= #000000 android:textsize= 20px / !-- More GUI components go here -- 6) pm_fragment.xml LinearLayout xmlns:android= http://schemas.android.com/apk/res/android android:orientation= horizontal android:layout_height= fill_parent android:background= #666666 android:textcolor= #000000 android:textsize= 20px / !-- More GUI components go here -- 7) strings.xml resources string name= app_name exemplo12 /string string name= action_settings settings /string string name= landscape_message this is Landscape mode fragment /string string name= portrait_message this is Portrait mode fragment /string /resources 8) AndroidManifest.xml android:minsdkversion= 11 Comentários getresources().getconfiguration() = obtém as informações de configuração do equipamento como tamanho da tela, orientação, localização, escala, etc. getfragmentmanager() = retorna o FragmentManager para interagir com os fragmentos associados com a atividade. begintransaction( ) = inicia operações nos fragmentos associados com o FragmentManager. commit( ) = executa operação. replace( ) = substitui um fragmento por outro na interface. inflate( ) = expande um fragmento. android:baselinealigned= false = evita que o aplicativo tente alinhar os componentes filhos do layout. Apenas melhora o desempenho do aplicativo. android:layout_weight = define o peso de cada fragmento na divisão do layout. = o tamanho da tela é definido pelo layout_weight. Exemplo 13 divide a interface em dois fragmentos. 1) MainActivity.java package com.example.exemplo13; import android.support.v4.app.fragmentactivity; public class MainActivity extends FragmentActivity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); 2) MenuFragment.java package com.example.exemplo13; import android.app.listfragment; import android.widget.arrayadapter; import android.widget.listview; public class MenuFragment extends ListFragment { String[] AndroidOS = new String[] { Cupcake , Donut , Eclair , Froyo , Gingerbread , Honeycomb , Ice Cream SandWich , Jelly Bean , KitKat ; String[] Version = new String[]{ 1.5 , 1.6 , , 2.2 , 2.3 , , 4.0 , , 4.4 ; public View oncreateview(layoutinflater inflater,viewgroup container, Bundle savedinstancestate) { View view =inflater.inflate(r.layout.list_fragment, container, false); ArrayAdapter String adapter = new ArrayAdapter String (getActivity(), android.r.layout.simple_list_item_1, AndroidOS); setlistadapter(adapter); return view; public void onlistitemclick(listview l, View v, int position, long id) { TextFragment txt = (TextFragment)getFragmentManager().findFragmentById(R.id.fragment2); txt.change(androidos[position], version : +Version[position]); getlistview().setselector(android.r.color.holo_blue_dark); 3) TextFragment.java package com.example.exemplo13; import android.app.fragment; import android.widget.textview; public class TextFragment extends Fragment { TextView text,vers; public View oncreateview(layoutinflater inflater,viewgroup container, Bundle savedinstancestate) { View view = inflater.inflate(r.layout.text_fragment, container, false); text= (TextView) view.findviewbyid(r.id.androidos); vers= (TextView)view.findViewById(R.id.Version); return view; public void change(string txt, String txt1){ text.settext(txt); vers.settext(txt1); 4) activity_main.xml xmlns:tools= http://schemas.android.com/tools android:baselinealigned= false android:layout_height= match_parent android:orientation= horizontal tools:context= com.example.exemplo13.mainactivity android:layout_height= match_parent android:layout_weight= 1 class= com.example.exemplo13.menufragment android:layout_weight= 2 android:layout_height= match_parent class= com.example.exemplo13.textfragment 5) list_fragment.xml android:orientation= vertical android:layout_height= match_parent ListView / 6) text_fragment.xml android:orientation= vertical android:gravity= center android:background= #5ba4e5 android:layout_height= match_parent android:layout_width= wrap_content android:textsize= 40px android:textcolor= #ffffff android:layout_gravity= center android:layout_width= wrap_content android:layout_gravity= center android:textcolor= #ffffff android:textsize= 30px 7) AndroidManifest.xml android:minsdkversion= 11 Comentários getactivity( ) = retorna a atividade que o fragmento está associado. android.r.layout.simple_list_item_1 = constante interna do sistema. ArrayAdapter String (context (activity), XML Resource, data array) = converte um vetor de strings em itens de uma View. O método inflate( ) possui três argumentos: o layout a ser apresentado; o ViewGroup que é pai do layout a ser mostrado; valor booleano indicando se o layout deve ser atachado ao ViewGroup (quando o sistema já inseriu o layout, deve ser falso). setlistadapter( ) = posiciona cursor na View. onlistitemclick( ListView l, View v, int position, long id ) = este método é chamado quando um item da lista é selecionado. L = é o ListView onde o clique aconteceu; v = é o view que foi clicado dentro da ListView; position = é a posição dentro da lista; id = é o id da linha clicada. findfragmentbyid(int id) = identifica o fragmento. setselector( ) = marca item selecionado na lista.
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks