最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(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ó)家法律
宝应县| 咸阳市| 宜州市| 海兴县| 尤溪县| 新建县| 南召县| 前郭尔| 桐乡市| 呼和浩特市| 上饶市| 江安县| 平舆县| 安西县| 栾川县| 遂溪县| 新宁县| 宁波市| 乌鲁木齐县| 建水县| 大英县| 阳信县| 西宁市| 苍山县| 西藏| 洛浦县| 和田市| 江永县| 北宁市| 余干县| 海原县| 翁牛特旗| 定结县| 慈利县| 托克托县| 庆阳市| 资兴市| 安化县| 嫩江县| 昆山市| 汕头市|