如何堆叠 EditTexts

问题描述 投票:0回答:0

我做了一个游戏并为玩家名字添加了 EditTexts 但我想让它如果玩家名字很长并且填满了屏幕宽度那么名字应该堆叠在彼此之上 注意:TextViews 是使用 java 代码生成的

我试着询问 chatgpt,但它没有给我我想要的东西,我在这个网站上搜索了它,但问题是 TextViews 的内容发生了变化,所以我不知道如何让它工作

我使用的代码(如果有帮助的话)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    List<String> names = new ArrayList<>();
    LinearLayout nameLayout = findViewById(R.id.names);

    String[] playerNames = getIntent().getStringArrayExtra("player_names");

    for (String name : playerNames) {
        if (name.contains(",")) {
            String[] splitNames = name.split(",");
            names.addAll(Arrays.asList(splitNames));
        } else {
            names.add(name);
        }
    }

    String[] namesArray = names.toArray(new String[0]);

    for (int i = 0; i < playerNames.length; i++) {
        namesArray[i] = names.get(0).trim();
    }

    for (String name : playerNames) {
        TextView textView = new TextView(this);
        textView.setTextSize(20);
        textView.setText(name.trim());
        textView.setTextColor(getResources().getColor(R.color.yellow));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(20, 0, 0, 0);
        textView.setLayoutParams(params);
        nameLayout.addView(textView);
    }
}
java android textview
© www.soinside.com 2019 - 2024. All rights reserved.