【问题标题】:Set Layout Parameters on click button by edit Text dynamically by selecting view通过选择视图动态编辑文本,在单击按钮上设置布局参数
【发布时间】:2019-10-22 06:22:06
【问题描述】:

What I am exactly trying to do is there are two Button views in a view group and there are two edit boxes to get the height and width and a button 3 to apply changes, when a view is selected we can provide height and宽度,然后按按钮三以在所选视图按钮上应用更改...我使用了布局参数,一个按钮就完美了,但我不知道如何告诉按钮三选择哪个视图来应用布局更改。Image

【问题讨论】:

标签: android android-layout layout view parameters


【解决方案1】:

您必须将 onClickListener 设置为您想要调整大小的所有视图。

button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(someOnClickListener);
button2.setOnClickListener(someOnClickListener);

在这个someOnClickListeneronClick中,你要获取当前被点击的View的ID,并保存在一个变量中。

int id_of_the_view_that_was_clicked = view.getId();

上面获取视图ID的代码将来自OnClick()

完成此操作后,为您的按钮设置OnClickListener,单击该按钮时您希望调整另一个Views 的大小。

你可以这样做,

yourActivity.this.findViewById(id_of_the_view_that_was_clicked).setLayoutParams(layoutParams);

【讨论】:

  • 非常感谢你,它就像一个魅力先生......无法给你投票......可能是因为我是 Stack Overflow 的新手......再次感谢
  • 我如何对不同的按钮 onclick 使用相同的方法我尝试实现 View.onclick(覆盖方法)可能无法正常工作,因为 id_of_the_view_that_was_clicked 不是全局变量...这是我的代码link
  • 我会检查一下
  • docs.google.com/document/d/… 这是已编辑的。检查语法错误,因为它已经自动更正了一些关键字
猜你喜欢
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多