添加權限:java
網絡請求+網絡判斷+讀取外部儲存的權限android
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
加依賴(在最上面)buildToolsVersion "26.0.2"網絡
(最下面)圖片和網絡請求的依賴app
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.google.code.gson:gson:2.2.4'
====================================================================ide
佈局:佈局
<RelativeLayout 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.shenhuiran_0103.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/fl_main" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" /> <RadioGroup android:id="@+id/group" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="horizontal"> <RadioButton android:id="@+id/but1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="首頁" /> <RadioButton android:id="@+id/but2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="想法" /> <RadioButton android:id="@+id/but3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="市場" /> <RadioButton android:id="@+id/but4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="通知" /> <RadioButton android:id="@+id/but5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="更多" /> </RadioGroup> </LinearLayout> </RelativeLayout>
==========================================================ui
主要方法:google
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.FrameLayout; import android.widget.RadioGroup; import com.example.shenhuiran_0103.fragment.Main1Fragment; import com.example.shenhuiran_0103.fragment.Main2Fragment; import com.example.shenhuiran_0103.fragment.Main3Fragment; import com.example.shenhuiran_0103.fragment.Main4Fragment; import com.example.shenhuiran_0103.fragment.Main5Fragment; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private FrameLayout fl_main; private RadioGroup group; private ArrayList<Fragment> fragments; private FragmentManager supportFragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fl_main = (FrameLayout) findViewById(R.id.fl_main); group = (RadioGroup) findViewById(R.id.group); initFragments(); supportFragmentManager = getSupportFragmentManager(); supportFragmentManager.beginTransaction().add(R.id.fl_main, fragments.get(0)).commit(); //點擊按鈕處理切換邏輯 group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int id) { //i 就是你點擊的那個控件的id //supportFragmentManager能夠複用,Transaction不可複用,每次提交後事物就結束了; switch (id) { case R.id.but1: supportFragmentManager.beginTransaction().replace(R.id.fl_main, fragments.get(0)).commit(); break; case R.id.but2: supportFragmentManager.beginTransaction().replace(R.id.fl_main, fragments.get(1)).commit(); break; case R.id.but3: supportFragmentManager.beginTransaction().replace(R.id.fl_main, fragments.get(2)).commit(); break; case R.id.but4: supportFragmentManager.beginTransaction().replace(R.id.fl_main, fragments.get(3)).commit(); break; case R.id.but5: supportFragmentManager.beginTransaction().replace(R.id.fl_main, fragments.get(4)).commit(); break; } } }); } private void initFragments() { fragments = new ArrayList<>(); fragments.add(new Main1Fragment()); fragments.add(new Main2Fragment()); fragments.add(new Main3Fragment()); fragments.add(new Main4Fragment()); fragments.add(new Main5Fragment()); } }
=======================================
fragment方法:code
importandroid.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.example.shenhuiran_0103.R; public class Main1Fragment extends Fragment { private TabLayout myTab; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = View.inflate(getActivity(), R.layout.fragment_a, null); myTab = view.findViewById(R.id.mytab); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //添加選項卡 myTab.addTab(myTab.newTab().setText("天樞")); myTab.addTab(myTab.newTab().setText("天璇")); myTab.addTab(myTab.newTab().setText("天璣")); myTab.addTab(myTab.newTab().setText("天權")); myTab.addTab(myTab.newTab().setText("開陽")); myTab.addTab(myTab.newTab().setText("玉衡")); myTab.addTab(myTab.newTab().setText("瑤光")); //選項卡被選中的監聽事件 myTab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { Toast.makeText(getActivity(),tab.getText().toString(),Toast.LENGTH_SHORT).show(); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); } }
------------------------------------------------------------------------------------------xml
R.layout.fragment_a:
<!-- app:tabGravity="center" 設置居中對齊 app:tabIndicatorColor="@color/colorAccent" 下滑線的顏色 app:tabMode="scrollable" 設置tabLayoout菜單的模式,,,scrollable能夠橫向滾動 fixed:固定 app:tabSelectedTextColor="@color/colorPrimaryDark" 選中的時候文字的顏色 app:tabTextColor="@color/colorPrimary" 文本默認的顏色 --> <android.support.design.widget.TabLayout android:layout_height="50dp" android:layout_width="match_parent" android:id="@+id/mytab"> </android.support.design.widget.TabLayout>