简体   繁体   English

如何在相对布局中随机放置 textview 布局的 position?

[英]How to place textview in relative layout at random position of the layout?

I am facing the issue to place the textview on the random position in relative layout dynamically.我正面临将 textview 以相对布局动态放置在random position上的问题。 The total count of textview can be 1, 3, 4...or 30. It depend on the array list. textview 的总数可以是 1、3、4...或 30。这取决于数组列表。

I want to put text view randomly in relative layout .我想text view randomly in relative layout Please guide me to achive it.请指导我实现它。

try this:尝试这个:

   TextView textView = findViewById(R.id.textView);
    Random random = new Random();
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int random_width = random.nextInt(metrics.widthPixels - 
    textView.getWidth());
    int random_height = random.nextInt(metrics.heightPixels - 
    textView.getHeight());

    if (random_width > (metrics.widthPixels - 100)) {
        random_width -= 100;
    }

    if (random_height > (metrics.heightPixels - 200)) {
        random_height -= 200;
    }
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    layoutParams.width = metrics.widthPixels;
    layoutParams.height = metrics.heightPixels;

    layoutParams.setMargins(random_width, random_height, 0, 0);
    textView.setLayoutParams(layoutParams);


    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    layoutParams.width = metrics.widthPixels;
    layoutParams.height = metrics.heightPixels;


    layoutParams.setMargins(random_width, random_height, 0, 0);
    textView.setLayoutParams(layoutParams);

and in your xml:在你的 xml 中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"       
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HI"
        android:textColor="#ff9999" />


</RelativeLayout>

I suggest you next approach:我建议你下一个方法:

RelativeLayout relativeLayout = findViewById(R.id.relativeLayout);
Random random = new Random();
int num_textViews= 10;
for (int i = 0; i < num_textViews; ++i) {
    TextView textView = new TextView(this);
    textView.setX(random.nextInt(relativeLayout.getMeasuredWidth()));
    textView.setY(-random.nextInt(relativeLayout.getMeasuredHeight()));
    relativeLayout.addView(textView);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM