【问题标题】:How to place textview in relative layout at random position of the layout?如何将textview放置在布局的随机位置的相对布局中?
【发布时间】:2019-09-23 08:05:44
【问题描述】:

我正面临将文本视图以相对布局动态放置在random position 上的问题。 textview 的总数可以是 1, 3 , 4 ... 或 30。它取决于数组列表。

我想输入text view randomly in relative layout。请指导我实现它。

【问题讨论】:

  • 请向我们展示您到目前为止所做的工作。
  • 这与设计模式有什么关系? :D

标签: java android design-patterns


【解决方案1】:

我建议你下一个方法:

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);
}

【讨论】:

  • 使用显示尺寸而不是仅仅使用RelativeLayout的测量宽度和高度肯定会在某些时候导致一些奇怪的错误。
  • @Shadkirill 我已经实现了这个。但是出现这样的错误Caused by: java.lang.IllegalArgumentException: n must be positive。我该如何解决这个问题?
  • @Chirag Pragapati 你应该在你的布局已经被测量过的时候去做。因此,请尝试将您的代码移至 Activity 的 onResume() 方法。
  • 感谢@Shadkirill,我已经实现了mohammadReza Abiri's 答案。它正在工作,但有时 Textview 会显示在布局/屏幕之外。有时它会相互重叠。你能帮忙吗
  • @ChiragPrajapati 尝试使用 random.nextInt(relativeLayout.getMeasuredWidth()) 和 -random.nextInt(relativeLayout.getMeasuredHeight()) 来解决第一个问题。为了解决第二个问题,您应该保存已经生成的 textViews 位置并检查新生成的位置是否与已经生成的位置重叠。
【解决方案2】:

试试这个:

   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);

在你的 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>

【讨论】:

  • 谢谢@mohammadReza Abiri,我已经实现了。它正在工作,但有时 Textview 会显示在布局/屏幕之外。有时它会相互重叠。你能帮忙吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多