Android 新特性学习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 新特性学习相关的知识,希望对你有一定的参考价值。
概述
又到了项目学习的时间,今天所要学习的是一个新闻客户端,微阅,项目地址如下
微阅
我们开始学习吧。
笔记
首先我们来集成一个Bug管理平台
ToolBar
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
MaterialDesign推荐AppBar设置elevation为4dp.
点击按钮返回主页面
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.MyChildActivity"
android:label="@string/title_activity_child"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
<android.support.design.widget.TextInputLayout
android:id="@+id/textInput"
android:layout_
android:layout_>
<EditText
android:layout_
android:layout_
android:textColor="@android:color/black"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInput2"
android:layout_
android:layout_>
<EditText
android:layout_
android:layout_
android:textColor="@android:color/black"/>
</android.support.design.widget.TextInputLayout>
final TextInputLayout inputLayout = (TextInputLayout) findViewById(R.id.textInput);
final TextInputLayout inputLayout2 = (TextInputLayout) findViewById(R.id.textInput2);
inputLayout.setHint("请输入姓名");
inputLayout2.setHint("请输入地址");
EditText editText = inputLayout.getEditText();
EditText editText2 = inputLayout2.getEditText();
editText.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
if (s.length()>4)
inputLayout.setErrorEnabled(true);
Toast.makeText(MainActivity.this, "超过", Toast.LENGTH_SHORT).show();
inputLayout.setError("姓名长度不能超过4个");
else
inputLayout.setError(null);//这样这只可以保证每次都出现错误提示
inputLayout.setErrorEnabled(false);
@Override
public void afterTextChanged(Editable s)
);
editText2.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
if (s.length()>4)
inputLayout2.setErrorEnabled(true);
Toast.makeText(MainActivity.this, "超过", Toast.LENGTH_SHORT).show();
inputLayout2.setError("地址长度不能超过4个");
else
inputLayout2.setErrorEnabled(false);//只出现一次错误提示
@Override
public void afterTextChanged(Editable s)
);
//不自动消失
snackbar = Snackbar.make(inputLayout, "弹出提示", Snackbar.LENGTH_INDEFINITE);
public void click(View view)
snackbar.show();
snackbar.setAction("取消", new View.OnClickListener()
@Override
public void onClick(View v)
snackbar.dismiss();
);
TabLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fl_parent"
android:layout_
android:layout_
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="#00ff00"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_
android:layout_
app:layout_scrollFlags="scroll"
app:tabIndicatorColor="@android:color/holo_green_light"
app:tabSelectedTextColor="@android:color/holo_red_dark"
app:tabTextColor="@android:color/black" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_
android:layout_></android.support.v4.view.ViewPager>
</LinearLayout>
- TabLayout一般和ViewPager结合使用
- 代码中设置了指示器的颜色,选中和未选中文字的颜色
public class TabFragment extends Fragment
private View view;
private TextView textView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
view = inflater.inflate(R.layout.tab_item, null);
textView = (TextView) view.findViewById(R.id.tv_item);
return view;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
super.onActivityCreated(savedInstanceState);
//获取传过来的参数
String content = getArguments().getString("content", "");
textView.setText(content);
public class TabFragmentAdapter extends FragmentStatePagerAdapter
private List<Fragment> fragments;
private List<String> titles;
public TabFragmentAdapter(FragmentManager fm,List<Fragment> fragments,List<String> titles)
super(fm);
this.fragments = fragments;
this.titles = titles;
@Override
public Fragment getItem(int position)
return fragments.get(position);
@Override
public int getCount()
return fragments.size();
/**
* 获取Tab的标题
* @param position
* @return
*/
@Override
public CharSequence getPageTitle(int position)
return titles.get(position);
private ViewPager viewPager;
List<String> tabTitles = new ArrayList<>();
List<Fragment> fragments = new ArrayList<>();
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
tabLayout = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabTitles.add("Tab1");
tabTitles.add("Tab2");
tabTitles.add("Tab3");
tabTitles.add("Tab4");
tabTitles.add("Tab5");
tabTitles.add("Tab6");
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.addTab(tabLayout.newTab().setText(tabTitles.get(0)));
tabLayout.addTab(tabLayout.newTab().setText(tabTitles.get(1)));
tabLayout.addTab(tabLayout.newTab().setText(tabTitles.get(2)));
for (int i = 0; i < tabTitles.size(); i++)
Fragment fragment = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString("content","内容"+i);
fragment.setArguments(bundle);
fragments.add(fragment);
TabFragmentAdapter adapter = new TabFragmentAdapter(getSupportFragmentManager(),fragments,tabTitles);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
@Override
public void onTabSelected(TabLayout.Tab tab)
int position = tab.getPosition();
//viewpager平滑过度。不是瞬间切换
viewPager.setCurrentItem(position,false);
@Override
public void onTabUnselected(TabLayout.Tab tab)
@Override
public void onTabReselected(TabLayout.Tab tab)
);
TabLayout更改样式的代码
public class FragmentOne extends Fragment
private TabLayout tabLayout;
private List<String> titleList ;
private String[] titleString = "英超","西甲","法甲","德甲","意甲","中超";
private TextView itemView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = View.inflate(getContext(),R.layout.fg_one,null);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
initTitle();
return view;
public void initTitle()
titleList = Arrays.asList(titleString);
for (int i = 0; i < titleList.size(); i++)
TabLayout.Tab tab = tabLayout.newTab();
tabLayout.setBackgroundResource(R.drawable.tab_bg);
tab.setCustomView(R.layout.tab_item_layout);
itemView = (TextView) tab.getCustomView().findViewById(R.id.tab_txt);
itemView.setText(titleList.get(i));
tabLayout.addTab(tab);
if(i == 0)
setTabColor(tab,android.R.color.white);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
@Override
public void onTabSelected(TabLayout.Tab tab)
setTabColor(tab,android.R.color.white);
@Override
public void onTabUnselected(TabLayout.Tab tab)
setTabColor(tab,android.R.color.black);
@Override
public void onTabReselected(TabLayout.Tab tab)
);
public void setTabColor(TabLayout.Tab tab,int color)
TextView selectViiew = (TextView) tab.getCustomView().findViewById(R.id.tab_txt);
selectViiew.setTextColor(getResources().getColor(color));
以上是关于Android 新特性学习的主要内容,如果未能解决你的问题,请参考以下文章