zoukankan      html  css  js  c++  java
  • 使用Fragment让程序界面一分为多

    Fragment类型的接口具有以下性质:

    1、程序运行界面可有由多个Fragment组成;

    2、每一个Fragment 都有各自独立的运行状态。

    3、在程序运行过程中,Fragment可以动态的加入和移除。

    <fragment android:id="@+id/fragGame"
        android:name="tw.android.GameFragment"
        android:layout_width="0dp" 
        android:layout_height="fill_parent" 
        android:layout_weight="1"
        />

    <fragment>便签使用注意事项

    1、fragment 的开头字母必须小写

    2、每一个<fragment>标签的都要设置android:id 的属性

    3、<fragment>的name属性必须制定使用Fragment类,其要加上完整的组件路径名称。

    一下代码是一个猜拳游戏:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="100dp"
        android:paddingRight="100dp"
        android:paddingTop="50dp"
        >
    <fragment android:id="@+id/fragGame"
        android:name="tw.android.GameFragment"
        android:layout_width="0dp" 
        android:layout_height="fill_parent" 
        android:layout_weight="1"
        />
    <!-- 在fragment 加外框 使用FrameLayout呀-->
    <FrameLayout android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/detailsElementBackground"
        >
    <fragment android:id="@+id/fragGameResult"  
        android:name="tw.android.GameResultFragment"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />
    </FrameLayout>
    </LinearLayout>

    game.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="400dp"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        >
    <TextView android:id="@+id/txtTitle"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/promptTitle"
        android:textSize="40sp"
        android:textColor="#FF00FF"
        android:textStyle="bold"
        android:layout_centerHorizontal="true"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />
    <TextView android:id="@+id/txtCom"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/promptComPlay"
        android:layout_below="@id/txtTitle"
        android:layout_alignLeft="@id/txtTitle"
        android:textSize="20sp"
        android:layout_marginBottom="20dp"
        />
    <TextView android:id="@+id/txtMyPlay"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/promptMyPlay"
        android:layout_below="@id/txtTitle"
        android:layout_alignRight="@id/txtTitle"
        android:textSize="20sp"
        android:layout_marginBottom="20dp"
        />
    <Button android:id="@+id/btnScissors"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/playScissors"
        android:layout_below="@id/txtMyPlay"
        android:layout_alignLeft="@id/txtMyPlay"
        android:textSize="20sp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        />
    <TextView android:id="@+id/txtComPlay"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text=""
        android:layout_below="@id/btnScissors"
        android:layout_alignLeft="@id/txtCom"
        android:textSize="30sp"
        android:textColor="#FF00FF"
        />
    <Button android:id="@+id/btnStone"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/playStone"
        android:layout_below="@id/btnScissors"
        android:layout_alignLeft="@id/btnScissors"
        android:textSize="20sp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        />
    <Button android:id="@+id/btnNet"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/playNet"
        android:layout_below="@id/btnStone"
        android:layout_alignLeft="@id/btnStone"
        android:textSize="20sp"
        android:paddingLeft="25dp"
        android:paddingRight="25dp"
        />
    <TextView android:id="@+id/txtResult"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/result"
        android:layout_below="@id/btnNet"
        android:layout_alignLeft="@id/txtCom"
        android:textSize="20sp"
        android:textColor="#0FFFFF"
        android:layout_marginTop="20dp"
        />
    </RelativeLayout>

    game_result.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="400dp"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        >
    <TextView android:id="@+id/txtTitle"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/promptTitle"
        android:textSize="40sp"
        android:textColor="#FF00FF"
        android:textStyle="bold"
        android:layout_centerHorizontal="true"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />
    <TextView android:id="@+id/txtCom"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/promptComPlay"
        android:layout_below="@id/txtTitle"
        android:layout_alignLeft="@id/txtTitle"
        android:textSize="20sp"
        android:layout_marginBottom="20dp"
        />
    <TextView android:id="@+id/txtMyPlay"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/promptMyPlay"
        android:layout_below="@id/txtTitle"
        android:layout_alignRight="@id/txtTitle"
        android:textSize="20sp"
        android:layout_marginBottom="20dp"
        />
    <Button android:id="@+id/btnScissors"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/playScissors"
        android:layout_below="@id/txtMyPlay"
        android:layout_alignLeft="@id/txtMyPlay"
        android:textSize="20sp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        />
    <TextView android:id="@+id/txtComPlay"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text=""
        android:layout_below="@id/btnScissors"
        android:layout_alignLeft="@id/txtCom"
        android:textSize="30sp"
        android:textColor="#FF00FF"
        />
    <Button android:id="@+id/btnStone"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/playStone"
        android:layout_below="@id/btnScissors"
        android:layout_alignLeft="@id/btnScissors"
        android:textSize="20sp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        />
    <Button android:id="@+id/btnNet"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/playNet"
        android:layout_below="@id/btnStone"
        android:layout_alignLeft="@id/btnStone"
        android:textSize="20sp"
        android:paddingLeft="25dp"
        android:paddingRight="25dp"
        />
    <TextView android:id="@+id/txtResult"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/result"
        android:layout_below="@id/btnNet"
        android:layout_alignLeft="@id/txtCom"
        android:textSize="20sp"
        android:textColor="#0FFFFF"
        android:layout_marginTop="20dp"
        />
    </RelativeLayout>

    GameFragment.java

    package tw.android;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class GameFragment extends Fragment {
    
        private Button mBtnScissors,
                       mBtnStone,
                       mBtnNet;
        private TextView mTxtComPlay,
                          mTxtResult;
        private TextView mEdtCountSet,
                         mEdtCountPlayerWin,
                         mEdtCountComWin,
                         mEdtCountDraw;
        private int miCountSet = 0,
                    miCountPlayerWin = 0,
                    miCountComWin = 0,
                    miCountDraw = 0;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            return inflater.inflate(R.layout.game, container, false);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);
    
            setupViewComponent();
        }
        
        private void setupViewComponent() {
            mTxtComPlay = (TextView)getView().findViewById(R.id.txtComPlay);
            mTxtResult = (TextView)getView().findViewById(R.id.txtResult);
            mBtnScissors = (Button)getView().findViewById(R.id.btnScissors);
            mBtnStone = (Button)getView().findViewById(R.id.btnStone);
            mBtnNet = (Button)getView().findViewById(R.id.btnNet);
    
            mEdtCountSet = (EditText)getActivity().findViewById(R.id.edtCountSet);
            mEdtCountPlayerWin = (EditText)getActivity().findViewById(R.id.edtCountPlayerWin);
            mEdtCountComWin = (EditText)getActivity().findViewById(R.id.edtCountComWin);
            mEdtCountDraw = (EditText)getActivity().findViewById(R.id.edtCountDraw);
    
            mBtnScissors.setOnClickListener(btnScissorsLin);
            mBtnStone.setOnClickListener(btnStoneLin);
            mBtnNet.setOnClickListener(btnNetLin);
        }
    
        private Button.OnClickListener btnScissorsLin = new Button.OnClickListener() {
            public void onClick(View v) {
                // Decide computer play.
                int iComPlay = (int)(Math.random()*3 + 1);
                
                miCountSet++;
                mEdtCountSet.setText(new Integer(miCountSet).toString());
    
                // 1 - scissors, 2 - stone, 3 - net.
                if (iComPlay == 1) {
                    mTxtComPlay.setText(R.string.playScissors);
                    mTxtResult.setText(getString(R.string.result) +
                                      getString(R.string.playerDraw));
                    miCountDraw++;
                    mEdtCountDraw.setText(new Integer(miCountDraw).toString());
                }
                else if (iComPlay == 2) {
                    mTxtComPlay.setText(R.string.playStone);
                    mTxtResult.setText(getString(R.string.result) +
                              getString(R.string.playerLose));
                    miCountComWin++;
                    mEdtCountComWin.setText(new Integer(miCountComWin).toString());
                }
                else {
                    mTxtComPlay.setText(R.string.playNet);
                    mTxtResult.setText(getString(R.string.result) +
                              getString(R.string.playerWin));
                    miCountPlayerWin++;
                    mEdtCountPlayerWin.setText(new Integer(miCountPlayerWin).toString());
                }
            }
        };
        
        private Button.OnClickListener btnStoneLin = new Button.OnClickListener() {
            public void onClick(View v) {
                int iComPlay = (int)(Math.random()*3 + 1);
                
                miCountSet++;
                mEdtCountSet.setText(new Integer(miCountSet).toString());
    
                // 1 - scissors, 2 - stone, 3 - net.
                if (iComPlay == 1) {
                    mTxtComPlay.setText(R.string.playScissors);
                    mTxtResult.setText(getString(R.string.result) +
                              getString(R.string.playerWin));
                    miCountPlayerWin++;
                    mEdtCountPlayerWin.setText(new Integer(miCountPlayerWin).toString());
                }
                else if (iComPlay == 2) {
                    mTxtComPlay.setText(R.string.playStone);
                    mTxtResult.setText(getString(R.string.result) +
                              getString(R.string.playerDraw));
                    miCountDraw++;
                    mEdtCountDraw.setText(new Integer(miCountDraw).toString());
                }
                else {
                    mTxtComPlay.setText(R.string.playNet);
                    mTxtResult.setText(getString(R.string.result) +
                              getString(R.string.playerLose));
                    miCountComWin++;
                    mEdtCountComWin.setText(new Integer(miCountComWin).toString());
                }
            }
        };
        
        private Button.OnClickListener btnNetLin = new Button.OnClickListener() {
            public void onClick(View v) {
                int iComPlay = (int)(Math.random()*3 + 1);
                
                miCountSet++;
                mEdtCountSet.setText(new Integer(miCountSet).toString());
    
                // 1 - scissors, 2 - stone, 3 - net.
                if (iComPlay == 1) {
                    mTxtComPlay.setText(R.string.playScissors);
                    mTxtResult.setText(getString(R.string.result) +
                              getString(R.string.playerLose));
                    miCountComWin++;
                    mEdtCountComWin.setText(new Integer(miCountComWin).toString());
                }
                else if (iComPlay == 2) {
                    mTxtComPlay.setText(R.string.playStone);
                    mTxtResult.setText(getString(R.string.result) +
                              getString(R.string.playerWin));
                    miCountPlayerWin++;
                    mEdtCountPlayerWin.setText(new Integer(miCountPlayerWin).toString());
                }
                else {
                    mTxtComPlay.setText(R.string.playNet);
                    mTxtResult.setText(getString(R.string.result) +
                              getString(R.string.playerDraw));
                    miCountDraw++;
                    mEdtCountDraw.setText(new Integer(miCountDraw).toString());
                }
            }
        }
    }

    GameReulstFragment.java

    package tw.android;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class GameResultFragment extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            return inflater.inflate(R.layout.game_result, container, false);
        }
    
    }
  • 相关阅读:
    oracle中统计邮箱的总类
    java中形参的值传递
    There is no Action mapped for action name的解决方法
    java操作excel
    验证邮箱地址是否真实有效
    Tomcat服务器内外网通过IP地址都不能访问
    HashMap遍历的两种方式(转)
    用java实现栈
    oracle中利用merge语句防止重复插入
    sshkeyken 中文手册
  • 原文地址:https://www.cnblogs.com/zhoujn/p/4153898.html
Copyright © 2011-2022 走看看