[英]Android Floating Button not showing
I try to make drawing page with canvas and add floating button in corner for some actions. 我尝试用画布制作绘图页面,并在角落添加浮动按钮以执行某些操作。 But it is not appearing.
但它没有出现。 This is my classes
这是我的课程
DrawView.java DrawView.java
public class DrawView extends View implements View.OnTouchListener {
private List<Point> points = new ArrayList<>();
private Paint paint = new Paint();
public DrawView(Context c) {
super(c);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas) {
for (Point point : points) {
canvas.drawCircle(point.x, point.y, 30, paint);
}
}
public boolean onTouch(View view, MotionEvent event) {
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
points.add(point);
invalidate();
return true;
}}
And Main activity 和主要活动
public class DrawingPage extends AppCompatActivity {
private DrawView drawView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawing_page);
drawView = new DrawView(getApplicationContext());
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
drawView.requestFocus();
}}
XML XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="reminder.com.picsartdrawing.DrawingPage">
<android.support.design.widget.FloatingActionButton
android:id="@+id/myFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:elevation="4dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
But floating button is not appearing, any solutions? 但浮动按钮没有出现,任何解决方案? Screenshot
截图
EDIT!!! 编辑!!! in Activity's onCreate method I have changed this, and now I have drawing canvas page with button.
在Activity的onCreate方法中我已经改变了这一点,现在我已经用按钮绘制了画布页面。
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
drawView = new DrawView(getApplicationContext());
drawView.setBackgroundColor(Color.WHITE);
drawView.requestFocus();
assert layout != null;
layout.addView(drawView);
在onCreate()
,用setContentView()
替换“主”视图2次,所以只试试这个:
setContentView(R.layout.activity_drawing_page);
You need to add this in the onCreate method of your activity class: 您需要在活动类的onCreate方法中添加它:
FloatingActionButton fab = (FloatingActionButton) findViewById (R.id.fab);
fab.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {
Snackbar.make (view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction ("Action", null).show ();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.