android 悬浮按钮 功能实现,怎么在Android中利用FloatingActionButton实现一个悬浮按钮效果...

本文详细介绍了如何在Android中利用FloatingActionButton实现悬浮按钮效果,包括自定义TagView、组合Tag和FloatingActionButton的TagFabLayout,以及包含多个item的MultiFloatingActionButton的布局和动画效果实现。通过示例代码展示了如何布局、设置点击事件和属性动画,帮助读者理解整个实现过程。
摘要由CSDN通过智能技术生成

怎么在Android中利用FloatingActionButton实现一个悬浮按钮效果

发布时间:2020-12-02 17:41:30

来源:亿速云

阅读:238

作者:Leah

今天就跟大家聊聊有关怎么在Android中利用FloatingActionButton实现一个悬浮按钮效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

首先是这个最小的Tag:

af393e0844bee2657b92d857496c5b7c.png

这个Tag带文字,可以是一个TextView,但为了美观,我们使用CardView,CardView是一个FrameLayout,我们要让它具有显示文字的功能,就继承CardView自定义一个ViewGroup。

publicclass TagView extends CardView

内部维护一个TextView,在其构造函数中我们实例化一个TextView用来显示文字,并在外部调用setTagText的时候把TextView添加到这个CardView中。

public class TagView extends CardView {

private TextView mTextView;

public TagView(Context context) {

this(context, null);

}

public TagView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public TagView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

mTextView = new TextView(context);

mTextView.setSingleLine(true);

}

protected void setTextSize(float size){

mTextView.setTextSize(size);

}

protected void setTextColor(int color){

mTextView.setTextColor(color);

}

//给内部的TextView添加文字

protected void setTagText(String text){

mTextView.setText(text);

addTag();

}

//添加进这个layout中

private void addTag(){

LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT

, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);

int l = dp2px(8);

int t = dp2px(8);

int r = dp2px(8);

int b = dp2px(8);

layoutParams.setMargins(l, t, r, b);

//addView会引起所有View的layout

addView(mTextView, layoutParams);

}

private int dp2px(int value){

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP

, value, getResources().getDisplayMetrics());

}

}

接下来我们看这个item,它是一个tag和一个fab的组合:

13ec0a39c5c54f19f5d09fa6a9f9e580.png

tag使用刚才我们自定义的TagView,fab就用系统的FloatingActionButton,这里显然需要一个ViewGroup来组合这两个子View,可以使用LinearLayout,这里我们就直接使用ViewGroup。

public class TagFabLayout extends ViewGroup

我们为这个ViewGroup设置自定义属性,是为了给tag设置text:

在构造器中获取自定义属性,初始化TagView并添加到该ViewGroup中:

public TagFabLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

getAttributes(context, attrs);

settingTagView(context);

}

private void getAttributes(Context context, AttributeSet attributeSet){

TypedArray typedArray = context.obtainStyledAttributes(attributeSet

, R.styleable.FabTagLayout);

mTagText = typedArray.getString(R.styleable.FabTagLayout_tagText);

typedArray.recycle();

}

private void settingTagView(Context context){

m

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值