【问题标题】:Dynamically TextView Or buttons Android Studio动态 TextView 或按钮 Android Studio
【发布时间】:2021-05-18 15:03:54
【问题描述】:
我不知道该怎么做:
我有一个传递给我的新活动的变量。
这个变量是一个数字。收到的投掷意图会因用户而异。
所以我想根据 Number 变量动态编写按钮或 texViews。
示例:数字 = 4;
有 4 个按钮或文本视图(每个都有 onclick 侦听器,写入的文本有 Button 1、Button 2 等)。
示例编号 = 10;
有 10 个按钮或文本视图等(每个都有 onclick 监听器)。
不知道如何解决这个问题
【问题讨论】:
标签:
android-studio
button
textview
【解决方案1】:
您可以像这样创建新的动态视图:
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.yourLinearLayoutId);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// with addView method you say to your app when you want to add this view inside on your LinearLayout view
ll.addView(myButton, lp);