[英]Chaging Views' position in android
I'm trying to build a simple app where there's a button and each time it gets clicked it changes its location on the screen.我正在尝试构建一个简单的应用程序,其中有一个按钮,每次单击它都会改变它在屏幕上的位置。 I don't have any experience at all developing for android so I don't know how to change Views' properties etc.我根本没有任何开发 android 的经验,所以我不知道如何更改视图的属性等。
Try this and enjoy:)试试这个并享受:)
activity_main.xml 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
android:orientation="vertical"
tools:context=".MainActivity"
android:gravity="center">
<Button
android:id="@+id/my_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click !" />
</RelativeLayout>
MainActivity.kt 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()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.