【问题标题】:How to dynamically replace a button in Android?如何动态替换Android中的按钮?
【发布时间】:2018-07-24 22:16:36
【问题描述】:

我正在制作一个待办事项应用程序。基本上,我希望屏幕从顶部的添加按钮开始。当用户单击按钮时,按钮会向下滑动,用户可以输入刚好适合一/两行的字符。用户一次只能输入 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 属性可能已关闭。我会尝试通过谷歌搜索来修复它们,但如果你现在可以帮助我,我将不胜感激。考虑这是一个额外的问题。

【问题讨论】:

    标签: android android-layout listview


    【解决方案1】:

    如果您使用的是列表视图,则只需在列表视图中添加页脚。这里是小sn-p。

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

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      • 2020-06-16
      • 2015-04-19
      • 2017-11-04
      相关资源
      最近更新 更多