今天看到一篇好文章css
https://www.2cto.com/kf/201609/545979.htmlhtml
轉載過來記錄一下,日後須要的時候能夠隨時查看;android
接下來進入正題:數組
對於動態的使用fragment,就是簡單的底部多個按鈕,而後多個fragment進行切換,這個應該很簡單,平時都在用。緩存
MainActivity:app
/** * 主佈局 * @author Rine * @version 1.0, 2015-12-1 */ public class MainActivity extends FragmentActivity{ MainDB mdata = new MainDB(); /** * 定義結束時間 */ private long exitTime = 0; /** * 定義FragmentTabHost對象 */ private FragmentTabHost mTabHost; /** * 定義一個佈局 */ private LayoutInflater layoutInflater; /** * 定義數組來存放Fragment界面 */ private Class fragmentArray[] = mdata.fragmentArray; /** * 定義數組來存放按鈕圖片 */ private int mImageViewArray[] = mdata.ImageViewArray; /** * Tab選項卡的文字 */ private String mTextviewArray[] = mdata.TextviewArray; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // CloudOnlyDB dbHelper; // dbHelper = new CloudOnlyDB(MainActivity.this); // HomeData home = new HomeData(MainActivity.this); initView( ); } /** * 初始化組件 */ private void initView (){ /** * 實例化佈局對象 */ layoutInflater = LayoutInflater.from(this); /** * 實例化TabHost對象,獲得TabHost */ mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.container); //加載內容 /** * 獲得fragment的個數 */ int count = fragmentArray.length; for(int i = 0; i < count; i++){ TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i)); //爲每個Tab按鈕設置圖標、文字和內容 mTabHost.addTab(tabSpec, fragmentArray[i], null); //將Tab按鈕添加進Tab選項卡中 } } /** * 給Tab按鈕設置圖標和文字 */ private View getTabItemView(int index){ View view = layoutInflater.inflate(R.layout.main_tab_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.imageview); imageView.setImageResource(mImageViewArray[index]); TextView textView = (TextView) view.findViewById(R.id.textview); textView.setText(mTextviewArray[index]); return view; } /** * 2次退出效果 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { exit(); //按返回鍵,true則退出 return false; } return super.onKeyDown(keyCode, event); } public void exit() { //按返回退出 if ((System.currentTimeMillis() - exitTime) > 2000) { Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show(); exitTime = System.currentTimeMillis(); } else { //退出 finish(); System.exit(0); } } }
MainAcitivity 中的MainDBide
/** * 存放主佈局數據 * @author Rine * @version 1.0, 2015-12-1 */ public class MainDB { /** * 定義數組來存放Fragment界面 */ public Class fragmentArray[] = {one.class,two.class,three.class}; /** * 定義數組來存放按鈕圖片 */ public int ImageViewArray[] = {R.drawable.home,R.drawable.ccarticle, R.drawable.ccone}; /** * Tab選項卡的文字 */ public String TextviewArray[] = {"one","twom","three"}; }
MainActivity 相應的佈局:佈局
<!--?xml version="1.0" encoding="utf-8"?--> <!-- Rine --> <!-- 功能:主佈局 --> <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android"> <framelayout android:id="@+id/container" android:layout_height="0dp" android:layout_weight="1" android:layout_width="fill_parent"> <framelayout android:id="@android:id/tabcontent" android:layout_height="0dp" android:layout_weight="0" android:layout_width="0dp"> </framelayout></android.support.v4.app.fragmenttabhost> </framelayout></linearlayout>
相應的style:this
<style name="homework_tab_item" type="text/css"><item name="android:layout_width">0dp</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_weight">1</item> <item name="android:button">@null</item> <item name="android:gravity">center</item> <item name="android:textColor">@color/homework_tab_item_text_color</item></style>
相應的 color:spa
<!--?xml version="1.0" encoding="utf-8"?--> <selector xmlns:android="https://schemas.android.com/apk/res/android"> <item android:color="#000000" android:state_checked="true"> <item android:color="#757575"> </item></item></selector>
這就是簡單動態fragment的應該。這也是其中的一種方法。
而後就是在其fragment中再嵌套多個fragment,我採用的是利用單選按鈕來實現,固然單選按鈕外面還要包一層RadioGroup。
Activity:
/** * fragment嵌套fragment * @author Rine * @version 1.0, 2015-12-1 */ public class two extends Fragment implements OnClickListener { /** * 定義一個佈局 */ private LayoutInflater inflater; // private View rootView;// 緩存Fragment view private Context mainActivity; private TwoToOne twoToOne; private TwoToTwo twoToTwo; /** * one、two RadioGroup 控件 */ private RadioGroup twoGroup; protected RadioButton twoOne, twoTwo; /** * 加載頁面 */ @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mainActivity = getActivity(); inflater = LayoutInflater.from(getActivity()); // 初始化控件和聲明事件 // rootView = inflater.inflate(R.layout.two, null); twoGroup = (RadioGroup) getActivity().findViewById(R.id.two_group); twoOne = (RadioButton) getActivity().findViewById(R.id.two_one); twoTwo = (RadioButton) getActivity().findViewById(R.id.two_two); //控件顏色 twoOne.setTextColor(getResources().getColor(R.color.red)); twoTwo.setTextColor(getResources().getColor(R.color.black)); twoOne.setOnClickListener(this); twoTwo.setOnClickListener(this); setDefaultFragment(); } /** * 設置默認的Fragment */ private void setDefaultFragment() { FragmentManager fm = getFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); twoToOne = new TwoToOne(); transaction.add(R.id.framelayout, twoToOne).commit(); } @Override public void onClick(View v) { FragmentManager fm = getFragmentManager(); // 開啓Fragment事務 FragmentTransaction transaction = fm.beginTransaction(); switch (v.getId()) { case R.id.two_one: if (twoToOne == null) { twoToOne = new TwoToOne(); } // 使用當前Fragment的佈局替代id_content的控件 transaction.replace(R.id.framelayout, twoToOne); //控件顏色 twoOne.setTextColor(getResources().getColor(R.color.red)); twoTwo.setTextColor(getResources().getColor(R.color.black)); break; case R.id.two_two: if (twoToTwo == null) { twoToTwo = new TwoToTwo(); } transaction.replace(R.id.framelayout, twoToTwo); //控件顏色 twoOne.setTextColor(getResources().getColor(R.color.black)); twoTwo.setTextColor(getResources().getColor(R.color.red)); break; } // transaction.addToBackStack(); // 事務提交 transaction.commit(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.two, null); } }
其對應的佈局:
<!--?xml version="1.0" encoding="utf-8"?--> <linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android"> <relativelayout android:background="@color/white" android:layout_height="@dimen/height_title" android:layout_width="match_parent"> <linearlayout android:id="@+id/layout_brck" android:layout_height="match_parent" android:layout_marginleft="@dimen/margin_15dp" android:layout_width="wrap_content" android:orientation="horizontal"> </linearlayout> <radiogroup android:gravity="center" android:id="@+id/two_group" android:layout_centerinparent="true" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal"> <radiobutton android:background="@null" android:checked="true" android:id="@+id/two_one" android:layout_marginright="@dimen/margin_20dp" android:text="one" android:textcolor="@color/black" android:textsize="@dimen/title_textsize_22sp" style="@style/homework_tab_item"> <radiobutton android:background="@null" android:id="@+id/two_two" android:layout_margin="5dp" android:layout_marginleft="@dimen/margin_20dp" android:text="two" android:textcolor="@color/black" android:textsize="@dimen/title_textsize_22sp" style="@style/homework_tab_item"> </radiobutton></radiobutton></radiogroup> <view android:background="@color/br_title_color" android:layout_alignparentbottom="true" android:layout_height="0.1dp" android:layout_width="match_parent"> </view></relativelayout> <framelayout android:id="@+id/framelayout" android:layout_height="0dp" android:layout_weight="1" android:layout_width="match_parent"> </framelayout> </linearlayout>
OK。大致就是這樣了。示例圖以下: