toolbar+tablayout和标题联动(标题居中)
程序员文章站
2022-03-28 16:12:59
一、 getView是个视图 tab也是个视图代码: toolbar.setTitle(""); setSupportActionBar(toolbar); ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerlayout, toolbar, R.string.open, R.string.close); drawerlayo...
一、 getView是个视图 tab也是个视图
代码:
toolbar.setTitle("");
setSupportActionBar(toolbar);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerlayout, toolbar, R.string.open, R.string.close);
drawerlayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
//侧滑图片显示
navigation.setItemIconTintList(null);
drawerlayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View view, float v) {
int length = navigation.getRight();
content.setX(length);
}
@Override
public void onDrawerOpened(@NonNull View view) {
}
@Override
public void onDrawerClosed(@NonNull View view) {
}
@Override
public void onDrawerStateChanged(int i) {
}
});
View headerView = navigation.getHeaderView(0);
header = headerView.findViewById(R.id.header);
header.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerlayout.closeDrawer(Gravity.LEFT);
}
});
header.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 200);
return false;
}
});
navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.one:
//通知
Toast.makeText(MainActivity.this, "已发送通知,请注意查收", Toast.LENGTH_SHORT).show();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT > 26) {
NotificationChannel notificationChannel = new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(notificationChannel);
}
Notification build = new NotificationCompat.Builder(MainActivity.this, "id")
.setSmallIcon(R.drawable.chat)
.setContentTitle("通知")
.setContentText("")
.build();
notificationManager.notify(100, build);
break;
case R.id.five:
if (menuItem.getTitle().equals("日间模式")) {
navigation.setBackgroundColor(0);
drawerlayout.setBackgroundColor(R.drawable.cart);
menuItem.setTitle("夜间模式");
} else if (menuItem.getTitle().equals("夜间模式")) {
navigation.setBackgroundColor(-1);
drawerlayout.setBackgroundColor(0);
menuItem.setTitle("日间模式");
}
break;
}
return false;
}
});
ArrayList<Fragment> fragments = new ArrayList<>();
fragments.add(new HomeFragment());
fragments.add(new ShiWuFragment());
fragments.add(new MyFragment());
fragments.add(new CollectFragment());
fragments.add(new ProjectFragment());
viewpager.setAdapter(new MainAdapter(getSupportFragmentManager(), fragments));
tablayout.setupWithViewPager(viewpager);
View home = getView(R.drawable.home_select, "首页");
View home1 = getView(R.drawable.food_select, "食物");
View home2 = getView(R.drawable.my_select, "我的");
View home3 = getView(R.drawable.collect_select, "收藏");
View home4 = getView(R.drawable.project_item, "项目");
tablayout.getTabAt(0).setCustomView(home);
tablayout.getTabAt(1).setCustomView(home1);
tablayout.getTabAt(2).setCustomView(home2);
tablayout.getTabAt(3).setCustomView(home3);
tablayout.getTabAt(4).setCustomView(home4);
tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
TextView tv = tab.getCustomView().findViewById(R.id.title);
tv_title.setText(tv.getText().toString());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
private View getView(int select, String text) {
View root = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_item, null);
ImageView img = root.findViewById(R.id.img);
TextView title = root.findViewById(R.id.title);
img.setImageResource(select);
title.setText(text);
return root;
}
xml布局:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" //在外面打出来,然后粘贴进来,有这个标题即可居中
android:id="@+id/title"
android:text="首页"
android:textSize="25dp"
/>
</android.support.v7.widget.Toolbar>
toolbar.setTitle("");
title = (TextView) findViewById(R.id.title);
tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (tablayout.getTabAt(1).isSelected()) {
title.setText("知识体系"); //setText
} else if (tablayout.getTabAt(4).isSelected()) {
title.setText("项目");
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
二:如果用这种形式字体不能变色 用下面这种方法实现toolbar和tablayout联动
tablayout.getTabAt(0).setText("123").setIcon(R.drawable.konwledge_select);
tablayout.getTabAt(1).setText("456").setIcon(R.drawable.konwledge_select);
tablayout.getTabAt(2).setText("789").setIcon(R.drawable.konwledge_select);
tablayout.getTabAt(3).setText("147").setIcon(R.drawable.konwledge_select);
tablayout.getTabAt(4).setText("258").setIcon(R.drawable.konwledge_select);
tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tv_title.setText(tab.getText().toString());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
本文地址:https://blog.csdn.net/qq_46237697/article/details/107583884
上一篇: 九九乘法表制作
下一篇: AndroidX 迁移步骤,一篇就够了