fragment is for use many kinds of view in one activity.
detail...
This is a useful for wide display device similar tablet.
So I make a simple code.
Left of display is icon for move to website.
Right of display is web browser.
If you touched icon, browser is moved.
activity_main.xml
- <fragment
- android:id="@+id/fragment1"
- android:name="com.answerofgod.fragment.Fragment1"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_weight="1" />
- <fragment
- android:id="@+id/fragment2"
- android:name="com.answerofgod.fragment.Fragment2"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_weight="20" />
I added 'fragment' tag.
Number of 'fragment' tag is number of view.
Name means Java file below.
Layout_weight is range of view.
Fragment1.java
- public class Fragment1 extends Fragment {
- @Override
- Bundle savedInstanceState) {
- return inflater.inflate(R.layout.frag1,container,false);
- }
- }
This class inflate frag1.xml file.
'Fragment ' java file just to define of xml file(layout).
frag1.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:id="@+id/daum"
- android:background="@drawable/daum"
- android:gravity="center"
- android:layout_width="50dp"
- android:layout_height="50dp"/>
......
- </LinearLayout>
This is left of display for icon.
Fragment2.java
- public class Fragment2 extends Fragment {
- @Override
- Bundle savedInstanceState) {
- return inflater.inflate(R.layout.frag2,container,false);
- }
- }
This file inflate frag2.xml file.
frag2.xml
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="#ff00ff00"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <WebView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:id="@+id/webview"/>
- </LinearLayout>
This is left of display for web browser.
MainActivity.java
- public class MainActivity extends Activity implements OnClickListener{
- WebView webview;
- Button daum;
- Button naver;
- Button facebook;
- Button twit;
- String url="http://m.facebook.com";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- webview=(WebView)findViewById(R.id.webview);
..........
- }
- @Override
- // TODO Auto-generated method stub
- switch(v.getId()){
- case R.id.daum:
- url="http://m.daum.net";
- webview.loadUrl(url);
- break;
- .........
- .........
This file is to define all for app.
Function, define, casting.... all in this file.
No comments:
Post a Comment