Android 加上「;浮动;文本框
我想在工作应用程序中添加一个移动的“浮动”文本 我有一个具有一些基本布局的工作应用程序,它类似于:Android 加上「;浮动;文本框,android,textview,Android,Textview,我想在工作应用程序中添加一个移动的“浮动”文本 我有一个具有一些基本布局的工作应用程序,它类似于: <LinearLayout> <TextView /> <TextView /> </LinearLayout> 创建了一个服装视图来处理它: 公共类FloatingText扩展视图{ private View floatingLayoutView; public FloatingText(Context context)
<LinearLayout>
<TextView />
<TextView />
</LinearLayout>
private View floatingLayoutView;
public FloatingText(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
floatingLayoutView = inflater.inflate(R.layout.floating_text, null);
TextView tv = (TextView) floatingLayoutView
.findViewById(R.id.flaotingViewTextView);
tv.setText("+1");
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setTextColor(Color.RED);
tv.setTypeface(Typeface.MONOSPACE);
tv.bringToFront();
}
public View getFloatingLayoutView() {
return floatingLayoutView;
}
}
注意,我添加了一个get方法来接收视图。我相信这是一个可怕的解决方案。接下来,您可以看到我是如何使用它的:
- 在主要活动的onResume中添加以下代码:
RelativeLayout main = (RelativeLayout) findViewById(R.id.MainRelativeLayout); FloatingText ft = new FloatingText(this); final View floatingLayoutView = ft.getFloatingLayoutView(); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); lp.setMargins(140, 300, 0, 0); floatingLayoutView.setLayoutParams(lp); main.addView(floatingLayoutView);
再次重申,任何指点者和ISEA都将受到欢迎 正常翻译动画或ObjectAnimator或Viewproperty animator可用于此目的(注意:ObjectAnimator/Viewproperty animator在android版本3下不可用) 假设您的视图位于屏幕底部&屏幕高度为512px;可以通过(使用动画侦听器)执行向上平移动画:
你想过使用
Toast
消息吗?我想过,但Toast只是出现和消失,我希望能够控制它多一点。(例如,向上浮动)我刚刚看到了NineoAndRoids库的几个推荐版本,所以我将尝试一下。因此,如果您有任何一般性的评论,即使是对于更高的API,我也会非常满意…ViewPropertyAnimator
应该很好。您能否详细说明如何与工作应用程序一起使用?我读了一些文档,看到了一些示例,但我的主要问题是如何使视图“浮动”在布局中的其他元素之上。添加一个框架?以某种方式获得画布?使用ViewPropertyAnimator编辑我的答案,例如-谢谢你的额外代码。恐怕ViewPropertyAnimator是在API 12中添加的,所以我将继续寻找其他解决方案。
RelativeLayout main = (RelativeLayout) findViewById(R.id.MainRelativeLayout);
FloatingText ft = new FloatingText(this);
final View floatingLayoutView = ft.getFloatingLayoutView();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lp.setMargins(140, 300, 0, 0);
floatingLayoutView.setLayoutParams(lp);
main.addView(floatingLayoutView);
view.animate().translationYBy(-512).setListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
// after animation set visibility gone
view.setVisibility(View.GONE);
}
@Override
public void onAnimationCancel(Animator animation) {
view.setVisibility(View.GONE);
}
});