将功能从xml转换为java
基本上,我尝试在单击另一个按钮时动态添加按钮 当我点击“添加一个类”按钮时,我希望“数学”按钮像这样出现在它上面:,“数学”按钮只是它应该是什么样子的一个例子,它不是动态编程的 当前,当我单击“添加类”按钮时,会发生以下情况: 我希望在本例中动态添加的按钮“New button”看起来与“Math”相同 这是我的xml布局文件:将功能从xml转换为java,java,android,xml,xml-layout,Java,Android,Xml,Xml Layout,基本上,我尝试在单击另一个按钮时动态添加按钮 当我点击“添加一个类”按钮时,我希望“数学”按钮像这样出现在它上面:,“数学”按钮只是它应该是什么样子的一个例子,它不是动态编程的 当前,当我单击“添加类”按钮时,会发生以下情况: 我希望在本例中动态添加的按钮“New button”看起来与“Math”相同 这是我的xml布局文件: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:la
<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>
使用
newButton.setCompoundDrawables(0,0,R.drawable.rightarrow,0);
而不是第二次调用setBackgroundResource
。如果它看起来不太正确或没有显示图标,您也可以尝试setCompoundDrawableswithInInsitCBounds(0,0,R.drawable.rightarrow,0)
编辑
您仍然可以对这些按钮使用XML。使用以下内容创建一个XML文件(我们称之为“my_button.XML”):
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
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:textStyle="bold" />
好的,我得到了那部分谢谢!设置大小和边距/填充怎么样?我还需要一种方法使它倾斜而不是像素
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);
}
});
}
}
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
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:textStyle="bold" />
addClass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button newButton = (Button) getLayoutInflater().inflate(R.layout.my_button, buttonsLayout, false);
newButton.setText("New button");
buttonsLayout.addView(newButton, buttonsLayout.getChildCount() - 1);
}
});