仿抖音底部导航效果(一)
最终效果预览
这次实现的是第一步效果
<h3 style="color:blue">原理解析:通过对控件添加动画来实现仿抖音底部导航的效果</h3>
一.首先编写布局文件(这里是用TextView作为底部的指示横线)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:orientation="vertical" android:id="@+id/nav_top" android:layout_centerInParent="true" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:text="标题" android:id="@+id/nav_item_tv_title" android:textColor="#F1F1F1" android:textSize="16sp" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:id="@+id/nav_item_tv_line" android:layout_below="@id/nav_top" android:layout_marginTop="2dp" android:layout_alignRight="@id/nav_top" android:layout_alignLeft="@id/nav_top" android:background="#fff" android:alpha="0" android:layout_centerInParent="true" android:layout_height="2dp" /> </RelativeLayout>
二.创建NavItemView类继承RelativeLayout(这是最终代码展示,接下来讲解各种方法的效果【除了自定义属性】)
public class NavItemView extends RelativeLayout { TextView textView_title; TextView textView_line; private View mView; public NavItemView(Context context) { this(context,null); } public NavItemView(Context context, AttributeSet attrs) { this(context, attrs,0); } @SuppressLint("ResourceType") public NavItemView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mView = View.inflate(context, R.layout.view_navitem, this); textView_line=mView.findViewById(R.id.nav_item_tv_line); textView_title=mView.findViewById(R.id.nav_item_tv_title); // 获取自定义属性 TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.NavItem); if(ta!=null){ String navtext = ta.getString(R.styleable.NavItem_navtext); textView_title.setText(navtext); } } // 选中状态 public void startActive(){ textView_title.setTextColor(Color.WHITE); textView_title.setTextSize(18); textView_line.animate().alpha(1).setDuration(200).start(); } // 取消选中 public void cancelActive(){ textView_title.setTextColor(Color.parseColor("#F1F1F1")); textView_title.setTextSize(16); textView_line.animate().alpha(0).setDuration(200).start(); } }
三.startActive()方法作用:设置TextView的颜色为白色,并通过动画的方式显示底部指示线,达到选中状态。
四.cancelActive()方法作用:与startActive()方法的作用相反。
五.Activity调用
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ NavItemView navItemView1,navItemView2,navItemView3,navItemView4,navItemView_Temp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); navItemView1=findViewById(R.id.one); navItemView2=findViewById(R.id.two); navItemView3=findViewById(R.id.three); navItemView4=findViewById(R.id.four); navItemView1.setOnClickListener(this); navItemView2.setOnClickListener(this); navItemView3.setOnClickListener(this); navItemView4.setOnClickListener(this); } @Override public void onClick(View v) { if(navItemView_Temp!=null){ navItemView_Temp.cancelActive(); } switch (v.getId()){ case R.id.one: navItemView1.startActive(); navItemView_Temp=navItemView1; break; case R.id.two: navItemView2.startActive(); navItemView_Temp=navItemView2; break; case R.id.three: navItemView3.startActive(); navItemView_Temp=navItemView3; break; case R.id.four: navItemView4.startActive(); navItemView_Temp=navItemView4; break; } } }
六.自定义属性文件展示
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="NavItem"> <attr name="navtext" format="string"/> </declare-styleable> </resources>
七.Activity布局文件展示
<?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" android:background="#000" tools:context="com.example.douyin.MainActivity"> <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_height="40dp"> <com.example.douyin.NavItemView android:layout_width="0dp" android:id="@+id/one" app:navtext="首页" android:layout_weight="1" android:layout_height="wrap_content"> </com.example.douyin.NavItemView> <com.example.douyin.NavItemView android:layout_width="0dp" android:id="@+id/two" app:navtext="关注" android:layout_weight="1" android:layout_height="wrap_content"> </com.example.douyin.NavItemView> <com.example.douyin.NavItemView android:layout_width="0dp" android:layout_weight="1" android:id="@+id/three" app:navtext="消息" android:layout_height="wrap_content"> </com.example.douyin.NavItemView> <com.example.douyin.NavItemView android:layout_width="0dp" android:layout_weight="1" android:id="@+id/four" app:navtext="我" android:layout_height="wrap_content"> </com.example.douyin.NavItemView> </LinearLayout> </RelativeLayout>

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
信则有不信则无:区块链手机会是个伪命题吗?
随着区块链技术的走红,近期,某些厂商(包括手机和非手机厂商)先后发布了声称采用区块链技术的所谓区块链手机,并在业内引起了激烈的争论。那么区块链手机到底是一种真实的存在,还是厂商们为了商业目的宣传的噱头? 在此,我们可以先看看目前通过媒体报道可知的几家推出区块链手机的厂商(至少声称采用了与区块链相关的技术)的背景。国外厂商包括印度的VVDN、以色列的Sirin Labs和芬兰的Zipper Global;国内厂商包括糖果、长虹和联想。它们的共性是在目前的智能手机产业和市场中均已被边缘化,甚至有的根本就不是手机厂商。 由此不难看出,这些企业推出区块链手机的首要的目的就是希望借此改变自身在智能手机产业中的竞争位置,非主流的希望进入主流,不是手机厂商的希望借此能够敲开智能手机产业的大门,毕竟从未来若干时间看,智能手机依然是最主流的移动设备和流
- 下一篇
如何从一名iOS开发新手到iOS开发专家
虽然你有可能在一周内学习到iOS开发的本质,但想要精通iOS开发需则要花费更长的时间。问题是你如何从新手变成专家?在这篇文章中,我们主要就这个主题给出一些建议: 1. 练习,练习还是练习 iOS开发没有捷径可走。这是在讨论这个话题之前需要强调的东西。如果你仅仅在周末的4PM和5PM之间学习/练习编程,那么你不会成为一个熟练的iOS开发者。这么做可能会很有趣,但是它很难让你成为一个老练的专业的iOS开发者。除了极少数的人,大部分人都需要不断的练习和实践。如果你还没有准备好投入大量时间,这时候最好重新审视你的目标和理想。 2. 向其他人学习 提升开发技能和采用最佳实践的最好策略之一是学习别人的代码。这不仅仅意味着浏览Stack Overflow,但更重要的是其他开发者的各种开源类库和代码片段。 不管何时你一头扎进各种类库的学习,比如AFNetworking或者Magical Record,很关键的一点是不要被各种代码淹没。有可能你不理解各种库中的代码,但这不是真正的关键,重点是从一个更高的层次来看源码和尽可能多地学习,比如命名规范、最佳实践以及设计模式等。 除了学习别人的代码外,创建你自...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- SpringBoot2全家桶,快速入门学习开发网站教程
- Docker安装Oracle12C,快速搭建Oracle学习环境
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- 设置Eclipse缩进为4个空格,增强代码规范