0

我现在正在做一个项目。我在屏幕上有多个 TextView。我希望他们在打开应用程序时有一个随机的 x 和 y 坐标。

有没有像 TextView.x = 30 和 TextView = 30 这样的东西?

4

2 回答 2

1

将 TextViews 放在FrameLayout中,并使用android:layout_marginTopandroid:layout_marginLeft设置它们的坐标。FrameLayout 绝对从左上角定位其项目。您可以通过更改布局中项目的顺序来使用 z 轴 - 第一个项目位于第二个项目下方,依此类推......

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"/>
    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="60dp"/>
</FrameLayout>

您也可以从代码中动态设置它。

TextView textView = (TextView) findViewById(R.id.some_textview);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.topMargin = 10; // margin in pixels, not dps
layoutParams.leftMargin = 10; // margin in pixels, not dps
textView.setLayoutParams(layoutParams);
于 2012-10-11T14:13:24.183 回答
0
 TextView textView = (TextView) findViewById(R.id.display);
 textView.setLayoutParams(new AbsoluteLayout.LayoutParams(100,40, 100, 70));    

但是在这里我们只能使用AbsoluteLayout,它已被弃用!

于 2012-10-11T06:51:15.040 回答