我正在嘗試構建一個簡單的應用程式,其中有一個按鈕,每次單擊它時,它都會更改其在螢屏上的位置。我根本沒有任何為 android 開發的經驗,所以我不知道如何更改視圖的屬性等。
uj5u.com熱心網友回復:
試試這個并享受:)
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@ id/my_relativeLayout" // add id to RelativeLayout
tools:context=".MainActivity">
<Button
android:id="@ id/my_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click !" />
</RelativeLayout>
MainActivity.kt
binding.myBtn.setOnClickListener {
val random = Random
val relativeLayout = findViewById<RelativeLayout>(R.id.my_relativeLayout)
binding.show.x = random.nextInt(relativeLayout.width - binding.show.width).toFloat()
binding.show.y = random.nextInt(relativeLayout.height - binding.show.height).toFloat()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408070.html
標籤:
下一篇:Kotlin財產委托未按預期作業
