将功能从xml转换为java

时间:2014-05-27 03:50:18

标签: java android xml xml-layout

基本上我在点击另一个按钮时试图动态添加按钮

当我点击“添加课程”按钮时,我希望“数学按钮出现在它上面:Link,”数学“按钮只是它应该是什么样子的一个例子,它没有动态编程。

目前,当我点击“添加课程”按钮时,会发生以下情况: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>

Roundedcorners drawable文件:

<?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);

        }
    });
  }
}

1 个答案:

答案 0 :(得分:0)

使用newButton.setCompoundDrawables(0, 0, R.drawable.rightarrow, 0);代替对setBackgroundResource的第二次调用。如果它看起来不太正确或者没有显示图标,您也可以尝试setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.rightarrow, 0)

修改

您仍然可以将XML用于这些按钮。使用以下内容创建一个XML文件(让我们称之为&#34; my_button.xml&#34;):

<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);

    } 
});