簡體   English   中英

在 android 中更改視圖的 position

[英]Chaging Views' position in android

我正在嘗試構建一個簡單的應用程序,其中有一個按鈕,每次單擊它都會改變它在屏幕上的位置。 我根本沒有任何開發 android 的經驗,所以我不知道如何更改視圖的屬性等。

試試這個並享受:)

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

     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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM