在java中,将对象移动到定义的随机数量上
所以我在android/java中有一个在java中,将对象移动到定义的随机数量上,java,android,Java,Android,所以我在android/java中有一个TextView,我想沿着它所在的水平轴随机定位。这是我目前掌握的代码: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <View android:layout_width="100dp" //T
TextView
,我想沿着它所在的水平轴随机定位。这是我目前掌握的代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="100dp" //This is the width I want to randomize
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/n5"/>
我的目标是,通过将android:layout\u width=
行中的100dp
随机化,我可以将TextView
移动一定的随机量。有人知道怎么做吗
谢谢 直接回答您的问题:
1.给视图一个id
<View android:id="@+id/picture_stream"
android:layout_width="100dp"
android:layout_height="wrap_content" />
另见
您也可以使用边距/填充,而不是用另一个视图“推”它。emm。。。你想移动一个确定的随机数量的东西吗?看来你想做不可能的事。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/movingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/n5"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/movingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/n5"/>
</LinearLayout>
TextView movingTextView = (TextView) this.findViewById(R.id.movingTextView);
movingTextView.setPaddingRelative((int) 1+Math.random()*100,0,0,0);