问题描述
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val myCanvasView = MyCanvasView(this)
setContentView(R.layout.activity_main)
//R.layout.activity_main
// val button: Button = findViewById<Button>(R.id.button)
}
}
class MyCanvasView(context: Context) : View(context) {
private var path = Path()
// variable for holding the color to draw with
private val drawColor = ResourcesCompat.getColor(resources,R.color.colorPaint,null)
// To store the x and y coordinate where the user touched the screen
private var motionTouchEventX = 0f
private var motionTouchEventY = 0f
// the x and y coordinate where the user lifts their finger from the screen
private var currentX = 0f
private var currentY = 0f
private val touchTolerance = ViewConfiguration.get(context).scaledTouchSlop
private lateinit var extraCanvas: Canvas
private lateinit var extraBitmap: Bitmap
现在我想向这个视图添加一个按钮。我在 activity_mail.xml 文件中做了哪些更改
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="88dp"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="160dp"
android:text="@string/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/canvas"
class="MyCanvasView"
android:layout_width="415dp"
android:layout_height="488dp"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
tools:ignore="ExtraText,MissingClass,MissingConstraints"
tools:layout_editor_absoluteX="-2dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
我应该对代码进行哪些更改,以便可以显示一个按钮以及分配给画布视图的屏幕特定部分?
解决方法
ConstraintLayout 可以在稍后定义的视图上创建一个视图的约束。这是一个从上到下制作 button 和 your_view 的例子。
public function mount($slug)
{
$this->post = Post::where('slug',$slug)->first();
}
public function render(Post $post)
{
$post->incrementReadCount();
return view('livewire.post-show')->layout('layouts.guest');
}
}