-1

基本的に、別のボタンをクリックしたときにボタンを動的に追加しようとしています

「クラスを追加」ボタンをクリックすると、「数学」ボタンがその上に表示されるようにしたいと思います: 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 ドローアブル ファイル:

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

        }
    });
  }
}
4

1 に答える 1

0

newButton.setCompoundDrawables(0, 0, R.drawable.rightarrow, 0);への 2 番目の呼び出しの代わりに使用し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);

    } 
}); 
于 2014-05-27T04:30:26.083 に答える