有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓将功能从xml转换为java

基本上,我尝试在单击另一个按钮时动态添加按钮

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

当前,当我单击“添加类”按钮时,会发生以下情况:link

我希望在本例中动态添加的按钮“New button”看起来与“Math”相同

这是我的xml布局文件:

<ScrollView xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:background="#7BEDFC" >

<LinearLayout
    安卓:id="@+id/classesLayout"
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent"
    安卓:orientation="vertical"
    安卓:padding="10dp" >

    <TextView
        安卓:id="@+id/textView1"
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:gravity="center"
        安卓:text="Classes"
        安卓:textAppearance="?安卓:attr/textAppearanceLarge"
        安卓:textColor="#FFFFFF"
        安卓:textStyle="bold" />

    <Button
        安卓:id="@+id/bExample"
        安卓:layout_marginBottom="10dp"
        安卓:layout_marginTop="10dp"
        安卓:layout_height="40dp"
        安卓:layout_width="fill_parent"
        安卓:background="@drawable/roundedcorners"
        安卓:drawableRight="@drawable/rightarrow"
        安卓:gravity="left|center_vertical"
        安卓:text=" Math"
        安卓:textStyle="bold" />

    <Button
        安卓:id="@+id/bClass"
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:background="@drawable/roundedcorners"
        安卓:layout_marginBottom="10dp"
        安卓:layout_marginTop="10dp"
        安卓:text="Add a Class"
        安卓:textStyle="bold" />

</LinearLayout>

圆角可绘制文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:安卓="http://schemas.安卓.com/apk/res/安卓" >
<item 安卓:state_pressed="true" >
 <shape 安卓:shape="rectangle"  >
     <corners 安卓:radius="10dip" />
     <stroke 安卓:width="1dip" 安卓:color="#FFFFFF" />
     <gradient 安卓:angle="-90" 安卓:startColor="#FFFFFF" 
    安卓:endColor="#FFFFFF"  />            
 </shape>
</item>
<item 安卓:state_focused="true">
 <shape 安卓:shape="rectangle"  >
     <corners 安卓:radius="10dip" />
     <stroke 安卓:width="1dip" 安卓:color="#FFFFFF" />
     <solid 安卓:color="#FFFFFF"/>       
 </shape>
</item>  
<item >
<shape 安卓:shape="rectangle"  >
     <corners 安卓:radius="10dip" />
     <stroke 安卓:width="1dip" 安卓:color="#FFFFFF" />
     <gradient 安卓:angle="-90" 安卓:startColor="#FFFFFF" 
 安卓: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) 个答案

  1. # 1 楼答案

    使用newButton.setCompoundDrawables(0, 0, R.drawable.rightarrow, 0);而不是第二次调用setBackgroundResource。如果它看起来不太正确或没有显示图标,您也可以尝试setCompoundDrawablesWithIntrinsicBounds(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" />
    

    然后你就可以写了

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