0

ボタンをクリックすると、画面上の別のポイントに変換されるアプリがあります。

翻訳に使用したものはこちらです。

findViewById(R.id.clickButton).animate().translationX((float) (Math.random() * ( 100 - 400 )));
findViewById(R.id.clickButton).animate().translationY((float) (Math.random() * ( 100 - 400 )));

これは良いことですが、ボタンが部分的に画面からはみ出し、場合によっては他のウィジェットの上に移動することがあります。これを回避する方法はありますか?

4

1 に答える 1

0

必要な範囲で Math.random() 値を取得するには、これを追加する必要があります -:

 bttn1.animate().translationX((float) (randomFromInterval(100,800)));
 bttn2.animate().translationY((float) (randomFromInterval(200,500)));

 public int randomFromInterval(int from,int to)
 {
    return (int) Math.floor(Math.random()*(to-from+1)+from);
 }

また、ボタンを他のウィジェットの上に配置する場合は、相対的なレイアウトの最後にボタンを配置する必要があります。これは、xml レイアウトで記述したウィジェットに従ってビューの z 軸の可視性を管理するためです。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:text="Button3" />

</RelativeLayout>

上記のレイアウトでは、button1 を z 軸からの最上位ビューにする必要がある場合、button3 は z 軸からの最上位ビューであり、次のようにコードを再配置します。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:text="Button3" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button1" />


</RelativeLayout>

この説明であなたの疑問が解消されることを願っています。

于 2013-09-01T04:27:36.547 に答える