Java中的Android代码
如何在运行时在按钮中添加文本。我添加了android java代码和XML的全部代码。
private ViewGroup viewGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutTransition layoutTransition = new LayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGE_APPEARING);
viewGroup = (ViewGroup) findViewById(R.id.container);
viewGroup.setLayoutTransition(layoutTransition);
}
public void onClick(View view) {
viewGroup.addView(new Button(this));
}
XML代码
这是
.
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:onClick="onClick"
android:text="Button" />