【发布时间】:2018-11-14 04:38:17
【问题描述】:
如何获取动态添加视图的id?
我想在按钮单击时将按钮添加到HorizontalScrollview,但将其限制为三或四行,然后它将为另外三-四行重复创建新的TableLayout,如下所示 -
我怎样才能做到这一点?
下面是我的代码:
public void addNewButton(int ids, TableLayout table){
Button btn = new Button(challengemode.this);
btn.setId(ids);
btn.setText(String.valueOf("btn" + ids));
btn.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT));
table.addView(btn);
horizontalScrollView.removeAllViews();
horizontalScrollView.addView(table);
}
这里是 XML,
<HorizontalScrollView
android:id="@+id/horizontalScrollViewGCode"
android:layout_width="125pt"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:background="@color/colorPrimaryDark">
<!--TABLE LAYOUT 1-->
<TableLayout
android:id="@+id/tblLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:stretchColumns="*"
android:fillViewport="true"
android:orientation="vertical">
</TableLayout>
</HorizontalScrollView>
【问题讨论】:
-
为什么需要身份证?添加按钮时不能添加点击侦听器吗?
-
@Chito Monte 请清楚地解释您的问题是什么,例如“我想做 xxx,但我获得了 yyy”
-
可以添加点击监听。并且如果要确定是哪个索引按钮,可以在按钮创建时为其添加标签。
标签: java android android-tablelayout horizontalscrollview