Comment puis-je ajouter des images à un LinearLayout programmically?
Il y a un LinearLayout et certains Boutons dans mon projet. Je veux lors de chaque Bouton cliqué sur ajouter une petite image pour LinearLayout de drawable. Comment puis-je ajouter une image à un LinearLayout programmically?
C'est mon LinearLayout dans un HorizontalScrollView:
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
>
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
et voici mon activité:
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout notes = (LinearLayout) findViewById(R.id.LinearLayout_notes);
final ImageView notes_do = new ImageView(this);
notes_do.setBackgroundResource(R.drawable.notes_do);
new Thread(new Runnable() {
@Override
public void run() {
final ImageButton img_1 = (ImageButton) findViewById(R.id.img_1_);
img_1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if( event.getAction() == MotionEvent.ACTION_DOWN ) {
notes.addView(notes_do);
}
return true;
}
}); //end of ontouch
}
}).start(); //end of thread
} //end of oncreate
}//end of activity
Merci.
OriginalL'auteur Mahdi Jafarzadeh | 2013-01-29
Vous devez vous connecter pour publier un commentaire.
Je l'ajoute à ma Q.
ce type d'exception que vous obtenez?
Il dit: L'enfant spécifié dispose déjà d'un parent. Vous devez appeler removeView() sur le parent de l'enfant en premier.
pouvez-vous assurer que le total code de la classe
OriginalL'auteur Pinki
Vous pouvez faire un findViewById à la linearlayout et ajouter une imageview de façon dynamique dans la onclicklistener de bouton
OriginalL'auteur user936414
OriginalL'auteur Nimish Choudhary
Que vous souhaitez définir l'image dynamiquement, sur le click du bouton. vous pouvez ajouter l'affichage de l'image à la disposition linéaire
OriginalL'auteur Sahil Mahajan Mj