首页 > 解决方案 > 如何动态替换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 属性可能已关闭。我会尝试通过谷歌搜索来修复它们,但如果你现在可以帮助我,我将不胜感激。考虑这是一个奖励问题。

标签: androidandroid-layoutlistview

解决方案


在这里,如果您使用的是列表视图,那么只需在列表视图中添加页脚。这是小片段。

    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 创建按钮)


推荐阅读