Skip to content

Instantly share code, notes, and snippets.

@tendoasan
Last active April 27, 2019 03:15
Show Gist options
  • Save tendoasan/23c2fd47b1c91a90d1b0e07179afe137 to your computer and use it in GitHub Desktop.
Save tendoasan/23c2fd47b1c91a90d1b0e07179afe137 to your computer and use it in GitHub Desktop.
[Android-Activity]#Android
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
>
<include layout="@layout/toolbar_default"/>
<FrameLayout
android:id="@+id/fl_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_tab_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textColor="@drawable/selector_color_white_light"
android:textSize="@dimen/textSizeMedium"
tools:text="已预订" />
<TextView
android:id="@+id/tv_tab_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:background="@drawable/shape_rect_solid_white_radius_medium"
android:gravity="center"
android:includeFontPadding="false"
android:minWidth="14dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="@string/default_int_number"
android:textColor="@color/textColorDarkBlue"
android:textSize="@dimen/textSizeSmall" />
</LinearLayout>
// 修改系统字体大小
public abstract class KeepFontScaleActivity extends AppCompatActivity {
@Override
public void onConfigurationChanged(Configuration newConfig) {
if (newConfig.fontScale != 1)//非默认值
getResources();
super.onConfigurationChanged(newConfig);
}
@Override
public Resources getResources() {
Resources res = super.getResources();
if (res.getConfiguration().fontScale != 1) {//非默认值
Configuration newConfig = new Configuration();
newConfig.setToDefaults();//设置默认
res.updateConfiguration(newConfig, res.getDisplayMetrics());
}
return res;
}
}
<!--在menu文件夹下添加该文件-->
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/action_more"
android:title="@string/more"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="@drawable/ic_menu_more"
/>
</menu>
public class MenuActivity extends AppCompatActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_more, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_more:
// do something
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
/**
* Created by tendoasan on 2017/11/6.
* 带 Toolbar,托管单个 Fragment
*/
public abstract class SimpleFragmentActivity extends AppCompatActivity {
@BindView(R.id.toolbar)
Toolbar mToolbar;
@BindView(R.id.tv_toolbar_title)
TextView mTvToolbarTitle;
/*@LayoutRes
protected abstract int getLayoutId();*/
@LayoutRes
protected int getLayoutId(){
// TODO 子类必须覆写此方法
return R.layout.activity_simple_fragment;
}
protected void onViewCreated(){
setupToolbar();
addFragment();
}
private void setupToolbar(){
setSupportActionBar(mToolbar);
mTvToolbarTitle.setText(getToolbarTitleStr());
mTvToolbarTitle.setTextSize(20);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null){
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back);
}
mToolbar.setVisibility(isShowToolbar() ? View.VISIBLE : View.GONE);
}
protected void setToolbarTitle(String title){
if (TextUtils.isEmpty(title)) return;
if (mTvToolbarTitle != null){
mTvToolbarTitle.setText(title);
}
}
protected boolean isShowToolbar(){
return true;
}
protected String getToolbarTitleStr(){
return getString(R.string.no_info);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return true;
}
private void addFragment(){
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fl_fragment_container);
if (fragment == null){
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fl_fragment_container, fragment)
.commit();
}
}
protected void initEventAndData(){
}
protected abstract Fragment createFragment();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置布局
setContentView(getLayoutId());
ButterKnife.bind(this);
// 接收事件和初始化数据
initEventAndData();
// 实例化子视图
onViewCreated();
}
protected Fragment getFragment(){
FragmentManager fm = getSupportFragmentManager();
return fm.findFragmentById(R.id.fl_fragment_container);
}
public Toolbar getToolbar() {
return mToolbar;
}
}
public abstract class SimpleTabActivity extends AppCompatActivity {
@BindView(R.id.toolbar)
Toolbar mToolbar;
@BindView(R.id.tv_toolbar_title)
TextView mTvToolbarTitle;
@BindView(R.id.tab_layout)
TabLayout mTabLayout;
@BindView(R.id.view_pager)
ViewPager mViewPager;
private String[] mTabTitles;
private ArrayList<TextView> mTabCountViewsList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置布局
setContentView(getLayoutId());
ButterKnife.bind(this);
// 接收事件和初始化数据
initEventAndData();
// 实例化子视图
onViewCreated();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return true;
}
@LayoutRes
protected int getLayoutId() {
// TODO 子类必须覆写此方法
return R.layout.activity_simple_tab;
}
protected void initEventAndData() {
mTabCountViewsList = new ArrayList<>();
mTabTitles = getTabTitles();
}
protected void onViewCreated() {
setupToolbar();
mViewPager.setAdapter(getFragmentPagerAdapter());
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// TODO 通知 fragment 刷新
onPageChanged(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.setTabMode(TabLayout.MODE_FIXED);
mTabCountViewsList.clear();
// 初始化Tab标题与数量视图,添加数量视图引用
for (int i = 0; i < mTabLayout.getTabCount(); i++){
TabLayout.Tab tab = mTabLayout.getTabAt(i);
if (tab != null){
View customView = LayoutInflater.from(SimpleTabActivity.this)
.inflate(R.layout.inflate_tab, mTabLayout, false);
// 标题
TextView tv_tab_title = (TextView) customView.findViewById(R.id.tv_tab_title);
tv_tab_title.setText(mTabTitles[i]);
// 数量
TextView tv_tab_count = (TextView) customView.findViewById(R.id.tv_tab_amount);
tv_tab_count.setVisibility(isShowCount() ? View.VISIBLE : View.GONE);
mTabCountViewsList.add(tv_tab_count);
tab.setCustomView(customView);
}
}
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
View customView = tab.getCustomView();
if (customView != null){
TextView tv_tab_title = (TextView) customView.findViewById(R.id.tv_tab_title);
tv_tab_title.setSelected(true);
}
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
View customView = tab.getCustomView();
if (customView != null){
TextView tv_tab_title = (TextView) customView.findViewById(R.id.tv_tab_title);
tv_tab_title.setSelected(false);
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
protected void onPageChanged(int position){
}
private void setupToolbar() {
setSupportActionBar(mToolbar);
mTvToolbarTitle.setText(getToolbarTitleStr());
mTvToolbarTitle.setTextSize(20);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back);
}
mToolbar.setVisibility(isShowToolbar() ? View.VISIBLE : View.GONE);
}
protected String getToolbarTitleStr() {
return getString(R.string.no_info);
}
protected boolean isShowToolbar() {
return true;
}
protected abstract String[] getTabTitles();
protected abstract FragmentPagerAdapter getFragmentPagerAdapter();
protected boolean isShowCount(){
return true;
}
protected void updateTabCountViews(List<Integer> counts){
if (counts == null || counts.size() == 0) return;
if (mTabCountViewsList != null
&& mTabCountViewsList.size() != 0
&& counts.size() == mTabCountViewsList.size()){
for (int i = 0; i < mTabCountViewsList.size(); i++){
mTabCountViewsList.get(i).setText(NumUtils.getCountDisplay(counts.get(i)));
}
}
}
public Toolbar getToolbar() {
return mToolbar;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
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:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:titleTextColor="@android:color/white"
app:contentInsetStartWithNavigation="0dp"
>
<TextView
android:id="@+id/tv_toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="20dp"
tools:text="@string/no_info"
android:textColor="@color/textColorWhite"
android:textSize="@dimen/textSizeBig"
android:text="@string/no_info"
/>
</android.support.v7.widget.Toolbar>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment