基本上,当我单击另一个按钮时,我试图动态添加按钮
当我单击“添加类”按钮时,我希望“数学”按钮出现在它上方,如下所示:Link,“数学”按钮只是它应该是什么样子的一个例子,它不是动态编程的。
目前,当我单击“添加类”按钮时,会发生以下情况:链接
我希望在这种情况下动态添加的按钮“新按钮”看起来与“数学”相同
这是我的 xml 布局文件:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#7BEDFC" >
<LinearLayout
android:id="@+id/classesLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Classes"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<Button
android:id="@+id/bExample"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_height="40dp"
android:layout_width="fill_parent"
android:background="@drawable/roundedcorners"
android:drawableRight="@drawable/rightarrow"
android:gravity="left|center_vertical"
android:text=" Math"
android:textStyle="bold" />
<Button
android:id="@+id/bClass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/roundedcorners"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="Add a Class"
android:textStyle="bold" />
</LinearLayout>
圆角可绘制文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="10dip" />
<stroke android:width="1dip" android:color="#FFFFFF" />
<gradient android:angle="-90" android:startColor="#FFFFFF"
android:endColor="#FFFFFF" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="10dip" />
<stroke android:width="1dip" android:color="#FFFFFF" />
<solid android:color="#FFFFFF"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="10dip" />
<stroke android:width="1dip" android:color="#FFFFFF" />
<gradient android:angle="-90" android:startColor="#FFFFFF"
android:endColor="#FFFFFF" />
</shape>
</item>
</selector>
活动文件的相关部分:
public class MainActivity extends ActionBarActivity {
LinearLayout buttonsLayout;
Button addClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonsLayout = (LinearLayout) findViewById(R.id.classesLayout);
addClass = (Button) findViewById(R.id.bClass);
addClass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button newButton = new Button(MainActivity.this);
newButton.setBackgroundResource(R.drawable.roundedcorners);
newButton.setBackgroundResource(R.drawable.rightarrow);
newButton.setGravity(Gravity.CENTER_VERTICAL| Gravity.LEFT);
newButton.setText("New button");
buttonsLayout.addView(newButton, buttonsLayout.getChildCount() - 1);
}
});
}
}