android - 如何动态替换Android中的按钮?
问题描述
我正在制作一个待办事项应用程序。基本上,我希望屏幕从顶部的添加按钮开始。当用户单击按钮时,按钮会向下滑动,用户可以输入刚好适合一/两行的字符。用户一次只能输入 10-15 个这样的任务,按钮每次向下滑动相同的距离。我该怎么做?这是我编写的当前 xml 代码:
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.111" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/list_view" />
另外,我知道 listview 属性可能已关闭。我会尝试通过谷歌搜索来修复它们,但如果你现在可以帮助我,我将不胜感激。考虑这是一个奖励问题。
解决方案
在这里,如果您使用的是列表视图,那么只需在列表视图中添加页脚。这是小片段。
TextView text = new TextView(getApplicationContext());
text.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.text_color_light));
text.setText("Block");
text.setTextSize(Math.round(getResources().getDimension(R.dimen.pad_top_bottom)-1));
text.setPadding(pad+7,pad,pad,pad);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertMessage.newInstance(displayName,al,getApplicationContext()).show(getSupportFragmentManager(),"alert");
}
});
listView.addFooterView(text);
它肯定会起作用。从 xml 中删除 Button 小部件并动态添加它。(这里代替 textView 创建按钮)
推荐阅读
- java - 通过Java程序运行时如何在shell脚本中提供输入选择?
- python - 使用相同的 Jinja 模板和 Pandas 和 Excel 创建多个模板
- c# - Rigidbody2D.MovePosition 导致物理滞后,例如重力
- django - 如何通过模板标签内的中间模型的外键访问模型的“related_name”?
- python - 如何从en中删除列表中的元素,直到python中的条件变为真?
- nuxt.js - nuxtServerInit 中的 Apollo-Query 在一段时间后停止工作
- laravel - Carbon设置时区不改变时间戳值
- javascript - Summernote - 有序和无序列表的自定义下拉列表
- c# - 错误 415:不支持的媒体类型 - ASP.NET MVC
- amazon-web-services - 使用 graphql 解析来自 aws 的数据(不是存储在我们数据库中的数据)