五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

第五章 用戶(hù)界面基礎(chǔ)(使用Fragment實(shí)現(xiàn)Tab導(dǎo)航)

2018-11-06 21:02 作者:swiss126  | 我要投稿

參考資料:

《Android應(yīng)用程序開(kāi)發(fā)》ISBN 9787302283164

參考軟件:

Android Studio、Eclipse+ADT、Android SDK、JDK

使用Fragment實(shí)現(xiàn)Tab導(dǎo)航

http://blog.csdn.net/yaya_soft/article/details/9714011

http://blog.csdn.net/zjlovety/article/details/21519205

?

一、Tab效果

二、項(xiàng)目結(jié)構(gòu)

三、文件代碼

代碼1 activity_main.xml

<?xml version="1.0"?encoding="utf-8"?>
?<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"
?????
xmlns:app="http://schemas.android.com/apk/res-auto"
?????
xmlns:tools="http://schemas.android.com/tools"
?????
android:layout_width="match_parent"
?????
android:layout_height="match_parent"
?????
android:orientation="vertical"
?????
tools:context="com.example.ch04.MainActivity">
???? <LinearLayout
?????????
android:layout_width="match_parent"
?????????
android:layout_height="wrap_content"
?????????
android:orientation="horizontal">
???????? <TextView
?????????????
android:id="@+id/tv1"
?????????????
android:layout_width="0dip"
?????????????
android:layout_height="wrap_content"
?????????????
android:layout_weight="1"
?????????????
android:text="社會(huì)新聞"?/>
???????? <TextView
?????????????
android:id="@+id/tv2"
?????????????
android:layout_width="0dip"
?????????????
android:layout_height="wrap_content"
?????????????
android:layout_weight="1"
?????????????
android:text="娛樂(lè)新聞"?/>
???????? <TextView
?????????????
android:id="@+id/tv3"
?????????????
android:layout_width="0dip"
?????????????
android:layout_height="wrap_content"
?????????????
android:layout_weight="1"
?????????????
android:text="國(guó)際新聞"?/>
???????? <TextView
?????????????
android:id="@+id/tv4"
??????? ?????
android:layout_width="0dip"
?????????????
android:layout_height="wrap_content"
?????????????
android:layout_weight="1"
?????????????
android:text="體育新聞"?/>
???? </LinearLayout>
???? <LinearLayout
?????????
android:id="@+id/content"
?????????
android:layout_width="fill_parent"
?????????
android:layout_height="fill_parent"
?????????
android:orientation="horizontal">
???? </LinearLayout>
?</LinearLayout>

代碼2 MainActivy

package?com.example.ch04;
?
?import?android.app.FragmentManager;
?import?android.app.FragmentTransaction;
?import?android.support.v7.app.AppCompatActivity;
?import?android.os.Bundle;
?import?android.view.View;
?import?android.widget.LinearLayout;
?import?android.widget.TextView;
?import?android.widget.Toast;
?
?public class?MainActivity?extends?AppCompatActivity?implements ?View.OnClickListener{
?????private?LinearLayout?linearLayout;
?????private?TextView?tv1,tv2,tv3,tv4;
?????private?FragmentManager?fragmentManager;
?????private?FragmentTransaction?fragmentTransaction;
?????@Override
?????protected void?onCreate(Bundle savedInstanceState) {
?????????super.onCreate(savedInstanceState);
???????? setContentView(R.layout.activity_main);
?????????linearLayout?= (LinearLayout)?this.findViewById(R.id.content);
?????????tv1?= ?(TextView)?this.findViewById(R.id.tv1);
???????? ?tv2?= (TextView)?this.findViewById(R.id.tv2);
?????????tv3?= ?(TextView)?this.findViewById(R.id.tv3);
?????????tv4?= ?(TextView)?this.findViewById(R.id.tv4);
?????????tv1.setOnClickListener(this);
?????????tv2.setOnClickListener(this);
?????????tv3.setOnClickListener(this);
?????????tv4.setOnClickListener(this);
?????????fragmentManager?= getFragmentManager();
?????????fragmentTransaction?=?fragmentManager.beginTransaction();
?????????//第一次顯示的新聞
?????????fragmentTransaction.add(R.id.content,new?Fragment1());
?????????fragmentTransaction.commit();
???? }
?
?????@Override
?????public void?onClick(View v) {
?????????fragmentTransaction?=?fragmentManager.beginTransaction();
?????????switch?(v.getId()){
?????????????case?R.id.tv1:
???????????????? Toast.makeText(this,?"tv1", Toast.LENGTH_SHORT).show();
?????????????????fragmentTransaction.replace(R.id.content,new?Fragment1());
?????????????????break;
?????????????case?R.id.tv2:
???????????????? Toast.makeText(this,?"tv2", Toast.LENGTH_SHORT).show();
?????????????????fragmentTransaction.replace(R.id.content,new?Fragment2());
?????????????????break;
?????????????case?R.id.tv3:
?????????????????fragmentTransaction.replace(R.id.content,new?Fragment3());
?????????????????break;
?????????????case?R.id.tv4:
?????????????????fragmentTransaction.replace(R.id.content,new?Fragment4());
?????????????????break;
???????? }
?????????fragmentTransaction.commit();
???? }
?}?

代碼3 Fragment1??? Fragment2? Fragment3?Fragment4

package?com.example.ch04;
?import?android.os.Bundle;
?import?android.app.Fragment;
?import?android.view.LayoutInflater;
?import?android.view.View;
?import?android.view.ViewGroup;
?/**
??* A simple {@link?Fragment} ?subclass.
??*/
?public class?Fragment1?extends?Fragment {
?????public?Fragment1() {
?????????// Required empty public constructor
?????}
?????@Override
?????public?View onCreateView(LayoutInflater inflater, ViewGroup container,
????????????????????????????? Bundle ?savedInstanceState) {
?????????// Inflate the layout for this fragment
?????????return?inflater.inflate(R.layout.fragment_fragment1, container,?false);
???? }
?}

代碼4 layout/fragment_fragment1.xml ??

<FrameLayout ?xmlns: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"
?????tools:context="com.example.ch04.Fragment1">
?????<!--?TODO: Update blank fragment layout?-->
?????<TextView
?????????android:layout_width="match_parent"
?????????android:layout_height="match_parent"
?????????android:text="社會(huì)新聞的內(nèi)容"?/>
?</FrameLayout>


第五章 用戶(hù)界面基礎(chǔ)(使用Fragment實(shí)現(xiàn)Tab導(dǎo)航)的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
桦南县| 通化市| 长泰县| 安阳市| 三江| 黄骅市| 古交市| 承德市| 榆林市| 隆林| 合水县| 商南县| 萨迦县| 墨竹工卡县| 始兴县| 上蔡县| 乌兰察布市| 丰宁| 比如县| 博罗县| 宁化县| 巴林左旗| 刚察县| 洛宁县| 彩票| 喜德县| 新余市| 正宁县| 宝坻区| 博白县| 乌兰察布市| 荣成市| 报价| 南昌县| 山丹县| 太原市| 桦南县| 青浦区| 梁山县| 顺义区| 聊城市|