首先导入gson-2.6.2.jar架包,universal-image-loader-1.9.3-with-sources.jar架包,还有依赖Modle(mpulltorefresh),Tablayout(横向滑动菜单)的依赖
StreamToString工具类
public class StreamToString { public static String streamToStr(InputStream inputStream,String chartSet){ StringBuilder builder=new StringBuilder(); try { BufferedReader br=new BufferedReader(new InputStreamReader(inputStream,chartSet)); String con; while ((con=br.readLine())!=null){ builder.append(con); }
br.close();
}
catch (Exception e) { e.printStackTrace(); }
return builder.toString(); }}
MyTask类
public class MyTask extends AsyncTask<String,Void,String> { //声明接口对象 private jiekouBack jiekouback; private String str; //为内部类设置有参无参方法 //private MyTask(){} //定义有参构造方法 public MyTask(jiekouBack jiekouback) { this.jiekouback = jiekouback; } @Override protected String doInBackground(String... strings) { try { URL url=new URL(strings[0]); HttpURLConnection connection=(HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); if(connection.getResponseCode() == 200){ InputStream inputStream=connection.getInputStream(); //调用工具类中的静态方法 String str = StreamToString.streamToStr(inputStream, "utf-8"); return str; } else if(connection.getResponseCode()==301 || connection.getResponseCode()==302){ //拿到重定向的地址 String newUrl=connection.getHeaderField("location"); URL u=new URL(newUrl); HttpURLConnection connection1=(HttpURLConnection) u.openConnection(); connection1.setRequestMethod("GET"); connection1.setReadTimeout(5000); connection1.setConnectTimeout(5000); if(connection1.getResponseCode()==200){ InputStream inputStream=connection1.getInputStream(); //调用工具类中的静态方法把数据转成string类型 str = StreamToString.streamToStr(inputStream,"utf-8"); } } } catch (Exception e) { e.printStackTrace(); } return null; } //任务完成 @Override protected void onPostExecute(String str) { //解析,封装到bean,更新ui组件 if(str!=null){ jiekouback.updataUIjson(str); }else { } } //定义接口 public interface jiekouBack{ /** * 根据回传的json字符串,解析并更新页面组件 * @param */ //抽象方法 void updataUIjson(String str); } }
网络判断类
public class NetStateUtil { /* * 判断网络连接是否已开 * true 已打开 false 未打开 * */ public static boolean isConn(Context context){ boolean bisConnFlag=false; ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo network = conManager.getActiveNetworkInfo(); if(network!=null){ bisConnFlag=conManager.getActiveNetworkInfo().isAvailable(); } return bisConnFlag; } /** * 当判断当前手机没有网络时选择是否打开网络设置 * @param context */ public static void showNoNetWorkDlg(final Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setIcon(R.mipmap.ic_launcher) // .setTitle(R.string.app_name) // .setMessage("当前无网络").setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 跳转到系统的网络设置界面 Intent intent = null; // 先判断当前系统版本 if(android.os.Build.VERSION.SDK_INT > 10){ // 3.0以上 intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); }else{ intent = new Intent(); intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings"); } context.startActivity(intent); } }).setNegativeButton("知道了", null).show(); } }
MyApplaction工具类
public class MyApplaction extends Application{ @Override public void onCreate() { super.onCreate(); //自定义sd卡缓存目录 File file=new File(Environment.getExternalStorageDirectory()+"/images"); //初始化,,全局配置applction ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this) .memoryCacheExtraOptions(400, 700)//缓存图片最大的长和宽 .threadPoolSize(3)//线程池的数量 .threadPriority(4) .memoryCacheSize(20*1024*1024)//设置内存缓存区大小 .diskCacheSize(80*1024*1024)//设置sd卡缓存区大小 .diskCache(new UnlimitedDiscCache(file))//自定义sd卡缓存目录 .writeDebugLogs()//打印日志内容 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理 .build(); ImageLoader.getInstance().init(configuration); } }
Mainactivity布局(滑动切换图片)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.example.com.toutiao.MainActivity" android:orientation="vertical" android:background="#C7564E"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tiao" android:text="跳过" android:background="@drawable/shape_selector" android:layout_marginRight="20dp" android:layout_marginTop="20dp" android:layout_marginLeft="300dp" android:visibility="invisible"/> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="9" android:id="@+id/pager"/> <RadioGroup android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:gravity="center" android:orientation="horizontal" android:layout_gravity="center_horizontal"> <RadioButton android:checked="true" android:id="@+id/but1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/but2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/but3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/but4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> </RelativeLayout>
MainActivity代码
public class MainActivity extends AppCompatActivity { private RadioGroup group; private ViewPager pager; private Button tiao; private SharedPreferences sharedPreferences; private SharedPreferences.Editor edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //得到sharedPreferences sharedPreferences = getSharedPreferences("User", MODE_PRIVATE); edit = sharedPreferences.edit();
//存入状态值 boolean ischeck = sharedPreferences.getBoolean("istrue", false); if(ischeck){
//如果状态值为true就跳转 Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); finish(); } //找到控件 group = findViewById(R.id.group); tiao = findViewById(R.id.tiao); pager = findViewById(R.id.pager); //数据 List<Integer> list=new ArrayList<Integer>(); list.add(R.mipmap.a1); list.add(R.mipmap.b1); list.add(R.mipmap.c1); list.add(R.mipmap.e1); //为but按钮设置监听事件 tiao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { edit.putBoolean("istrue", true); edit.commit();//存入一个状态值 //跳转到页面 Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } }); //为pager设置适配器 PageAdapter adapter=new PageAdapter(MainActivity.this,list); pager.setAdapter(adapter); //为pager设置监听 pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch(position) { case 0: group.check(R.id.but1); break; case 1: group.check(R.id.but2); break; case 2: group.check(R.id.but3); break; case 3: group.check(R.id.but4); //图片滑到最后一个页面时显示按钮 tiao.setVisibility(View.VISIBLE); edit.putBoolean("istrue", true); edit.commit();//存入一个状态值 Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); break; default: break; } } @Override public void onPageScrollStateChanged(int state) { } }); } }
MainActivity适配器(滑动切换不轮播)
class PageAdapter extends PagerAdapter{ Context context; List<Integer> list; public PageAdapter(Context context, List<Integer> list) { this.context=context; this.list=list; } @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView image=new ImageView(context); image.setImageResource(list.get(position)); container.addView(image); return image; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }
MainActivity2布局(底部导航,以及点击导航切换页面)
<?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" tools:context="com.example.com.toutiao.Main2Activity" android:orientation="vertical">
<!--fragment的占位布局(需要切换几个页面就new几个fragment类和布局)--> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:id="@+id/frag" android:layout_weight="9" > </FrameLayout> <RadioGroup android:id="@+id/group2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <RadioButton android:id="@+id/but01" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:checked="true" android:drawableTop="@drawable/image_selector" android:gravity="center" android:text="主页" android:textColor="@color/but_color" /> <RadioButton android:id="@+id/but02" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/image_selector2" android:gravity="center_horizontal" android:text="视频" android:textColor="@color/but_color" /> <RadioButton android:id="@+id/but03" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/image_selector3" android:gravity="center_horizontal" android:text="微头条" android:textColor="@color/but_color" /> <RadioButton android:id="@+id/but04" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/image_selector4" android:gravity="center_horizontal" android:text="发布" android:textColor="@color/but_color" /> <RadioButton android:id="@+id/but05" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/image_selector5" android:gravity="center_horizontal" android:text="我的" android:textColor="@color/but_color" /> </RadioGroup> </LinearLayout>
Main2Activity代码
public class Main2Activity extends AppCompatActivity { private RadioGroup group; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //找到控件 group = findViewById(R.id.group2); //为group设置监听事件 group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch(i){
//底部导航要切换的fragment case R.id.but01: //fragment的占位布局的id getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment1()).commit(); break; case R.id.but02: getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment2()).commit(); break; case R.id.but03: getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment3()).commit(); break; case R.id.but04: getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment4()).commit(); break; case R.id.but05: getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment5()).commit(); break; default: break; } } }); //初进入页面先加载一个页面 getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment1()).commit(); } }
fragment01布局(头条的top搜索框)
<?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" tools:context="com.example.com.toutiao.Main2Activity" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_gravity="center_vertical" android:gravity="center" android:background="#BF0202" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="今日头条" android:layout_marginLeft="10dp" android:textSize="25sp" android:textColor="#fff" android:id="@+id/tou_text" android:layout_alignParentLeft="true"/> <EditText android:layout_width="200dp" android:layout_height="100dp" android:layout_toRightOf="@+id/tou_text" android:layout_marginLeft="40dp" android:layout_alignBottom="@+id/tou_text" android:background="@drawable/shape_top" android:drawableLeft="@drawable/ss" android:hint="搜索一下……" android:focusable="false" android:layout_marginRight="10dp" android:layout_alignParentRight="true"/> </RelativeLayout>
<!--横向滑动菜单-->
<android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tab" app:tabGravity="center" app:tabIndicatorColor="#fff" app:tabMode="scrollable" app:tabSelectedTextColor="@color/colorAccent" app:tabTextColor="@color/colorPrimary" ></android.support.design.widget.TabLayout>
<!--与Tablayout绑定可滑动和点击切换页面效果-->
<android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pager" ></android.support.v4.view.ViewPager> </LinearLayout>
Fragment1类代码
public class Fragment1 extends Fragment { private ViewPager pager; private TabLayout mytab; private List<String> list; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment01,container,false); //找到控件 pager = view.findViewById(R.id.pager); mytab = view.findViewById(R.id.tab); //显示数据 getdata(); //设置适配器 pager.setAdapter(new MyAdapter(getActivity().getSupportFragmentManager())); //将tablayout与viewpager关联 mytab.setupWithViewPager(pager);
//一次加载所有数据 pager.setOffscreenPageLimit(list.size()); return view; } //设置数据的方法 private void getdata(){ list=new ArrayList<String>(); list.add("头条"); list.add("社会"); list.add("国内"); list.add("国际"); list.add("娱乐"); list.add("体育"); list.add("军事"); list.add("科技"); list.add("财经"); list.add("时尚"); } //创建适配器得到方法 class MyAdapter extends FragmentPagerAdapter{ //有参构造 public MyAdapter(FragmentManager fm) { super(fm); } //得到Tablayout的头部 @Override public CharSequence getPageTitle(int position) { return list.get(position); } @Override public Fragment getItem(int position) {
//和Tablayout关联时可切换的fragment ContentFragment contentFragment=new ContentFragment(); //传值 Bundle bundle=new Bundle(); if(list.get(position).equals("头条")){ bundle.putString("type","top"); }else if(list.get(position).equals("社会")){ bundle.putString("type","shehui"); }else if(list.get(position).equals("国内")){ bundle.putString("type","guonei"); }else if(list.get(position).equals("国际")){ bundle.putString("type","guoji"); }else if(list.get(position).equals("娱乐")){ bundle.putString("type","yule"); }else if(list.get(position).equals("体育")){ bundle.putString("type","tiyu"); }else if(list.get(position).equals("军事")){ bundle.putString("type","junshi"); }else if(list.get(position).equals("科技")){ bundle.putString("type","keji"); }else if(list.get(position).equals("财经")){ bundle.putString("type","caijing"); }else if(list.get(position).equals("时尚")){ bundle.putString("type","shishang"); } contentFragment.setArguments(bundle); return contentFragment; } @Override public int getCount() { return list.size(); } } }
contentFragment布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:ptr="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.com.toutiao.Main2Activity" > <!--展示数据 刷新 加载更多--> <com.handmark.pulltorefresh.library.PullToRefreshListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pull" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="flip" ptr:ptrHeaderBackground="#383838" ptr:ptrHeaderTextColor="#FFFFFF"> </com.handmark.pulltorefresh.library.PullToRefreshListView> </RelativeLayout>
ContentFragment代码(切换Tablayout菜单显示不同数据)
public class ContentFragment extends Fragment{ private ViewPager f1_pager; private DisplayImageOptions options; private TextView top; private int type1=1; private PullToRefreshListView plistview; private List<MyGson.ResultBean.DataBean> list=new ArrayList<>(); String url2; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.content_layout,container,false); //找到控件 f1_pager = view.findViewById(R.id.pager); plistview = view.findViewById(R.id.pull); //定义接口url String url = "http://v.juhe.cn/toutiao/index?type="; String url1="&key=979b10aa8ead50f95e176358595ba0e7"; Bundle bundle = getArguments(); String type = bundle.getString("type");
//接口拼接 url2=url+type+url1;
//首先加载数据
requestData();
plistview.setMode(PullToRefreshBase.Mode.BOTH);//支持上拉加载更多,下拉刷新 //为plistview设置监听 plistview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { type1=1;//下拉刷新 requestData(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { type1=2;//上拉加载更多 requestData(); } }); return view; }
//请求数据的方法 private void requestData() { MyTask myTask = new MyTask(new MyTask.jiekouBack() { @Override public void updataUIjson(String str) { Gson gson = new Gson(); MyGson myGson = gson.fromJson(str, MyGson.class); MyGson.ResultBean result = myGson.getResult(); if(type1==1){ list.clear();//清空list } List<MyGson.ResultBean.DataBean> list1 = result.getData(); list.addAll(list1);//加载全部 new Handler().postDelayed(new Runnable() { @Override public void run() { plistview.onRefreshComplete();//关闭头尾布局 } },1000); MyAdapter adapter = new MyAdapter(getActivity(), list); plistview.setAdapter(adapter); } }); myTask.execute(url2); } //异步加载图片 }
MyAdapter适配器
class MyAdapter extends BaseAdapter{ private final int ONE=0; private final int TWO=1; private final int TREE=2; Context context; List<MyGson.ResultBean.DataBean> list; private final DisplayImageOptions options; public MyAdapter(Context context, List<MyGson.ResultBean.DataBean> list) { this.list=list; this.context=context; options = new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时 .showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 1px=2字节 .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式 //.displayer(new RoundedBitmapDisplayer(100))//设置圆角 30代表半径 自定义 .build(); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public int getItemViewType(int position) { //得到数据中的每个图片 String image1 = list.get(position).getThumbnail_pic_s(); String image2 = list.get(position).getThumbnail_pic_s02(); String image3 = list.get(position).getThumbnail_pic_s03(); if(image1!=null && image2==null && image3==null){ return ONE; }else if(image1!=null && image2!=null && image3==null){ return TWO; }else{ return TREE; } } @Override public int getViewTypeCount() { return 3; } @Override public View getView(int i, View view, ViewGroup viewGroup) { int type = getItemViewType(i); if(type==ONE){ viewHolder1 holder1; if(view==null){ view=View.inflate(context,R.layout.list_layout,null); holder1=new viewHolder1(); holder1.image1=view.findViewById(R.id.list_image); holder1.text1=view.findViewById(R.id.list_text001); holder1.text2=view.findViewById(R.id.list_text002); view.setTag(holder1); }else{ holder1= (viewHolder1) view.getTag(); } ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),holder1.image1,options); holder1.text1.setText(list.get(i).getTitle()); holder1.text1.setText(list.get(i).getAuthor_name()); return view; }else if(type==TWO){ viewHolder2 holder2; if(view==null){ view=View.inflate(context,R.layout.list_layout2,null); holder2=new viewHolder2(); holder2.image21=view.findViewById(R.id.list_image21); holder2.image22=view.findViewById(R.id.list_image22); holder2.text21=view.findViewById(R.id.list_text21); holder2.text22=view.findViewById(R.id.list_text22); view.setTag(holder2); }else{ holder2= (viewHolder2) view.getTag(); } ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),holder2.image21,options); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s02(),holder2.image22,options); holder2.text21.setText(list.get(i).getTitle()); holder2.text22.setText(list.get(i).getAuthor_name()); return view; }else { viewHolder3 holder3; if(view==null){ view=View.inflate(context,R.layout.list_layout3,null); holder3=new viewHolder3(); holder3.image31=view.findViewById(R.id.list_image31); holder3.image32=view.findViewById(R.id.list_image32); holder3.image33=view.findViewById(R.id.list_image33); holder3.text31=view.findViewById(R.id.list_text31); holder3.text32=view.findViewById(R.id.list_text32); view.setTag(holder3); }else{ holder3= (viewHolder3) view.getTag(); } ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),holder3.image31,options); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s02(),holder3.image32,options); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s03(),holder3.image33,options); holder3.text31.setText(list.get(i).getTitle()); holder3.text32.setText(list.get(i).getAuthor_name()); return view; } } private class viewHolder1{ ImageView image1; TextView text1; TextView text2; } private class viewHolder2{ ImageView image21; ImageView image22; TextView text21; TextView text22; } private class viewHolder3{ ImageView image31; ImageView image32; ImageView image33; TextView text31; TextView text32; } }
清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.com.toutiao"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:name=".MyApplaction" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Main2Activity" /> <activity android:name=".Main3Activity"></activity> </application> </manifest>
相关选择器
1.按钮按下时和默认时的设置(在drawable下创建一个selector)
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@color/colorbule"></item> <item android:state_pressed="false" android:drawable="@color/coloryellow"></item> <item android:drawable="@color/colorAccent"></item> </selector>2. 今日头条底部导航按钮点击按钮变色
2.1按钮变色(在dawable下创建selector)
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="false" android:drawable="@drawable/zhu_select"></item> <item android:state_checked="true" android:drawable="@drawable/zhu_no"></item> </selector>2.2字体变色(在res文件夹下创建一个color文件夹)
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#f0f" android:state_checked="true"/> <item android:color="#000" android:state_checked="false"/> </selector>3.button按钮设置圆角边框,填充色(在drawable下创建shape)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="1dp" android:color="#ff0" ></stroke> <corners android:radius="5dp" ></corners> <solid android:color="#fff"></solid> </shape>