5

私のプロジェクトにはLinearLayoutといくつかのボタンがあります。各ボタンがクリックされたときに、ドローアブルからLinearLayoutに小さな画像を追加したいと思います。プログラムでLinearLayoutに画像を追加するにはどうすればよいですか?

これは、Horizo​​ntalScrollViewのLinearLayoutです。

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

そしてここに私の活動があります:

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
4

4 に答える 4

14
    LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout1);
for(int i=0;i<5;i++)
{
        ImageView ii= new ImageView(this);
        ii.setBackgroundResource(R.drawable.ic_action_search);
        ll.addView(ii);
}
于 2013-01-29T12:19:29.667 に答える
3

linearlayout に対して findViewById を実行し、ボタンの onclicklistener で imageview を動的に追加できます。

LinearLayout linLay = (LinearLayout)findViewById(R.id.LinearLayout1);
ImageView image = new ImageView(this);
//add image to imageview
...
linLay.addView(image); 
于 2013-01-29T12:19:12.163 に答える
1
        ImageView img = new ImageView( this);
        LinearLayout rl = (LinearLayout) findViewById(R.id.LinearLayout1);
        LinearLayout.LayoutParams viewParamsCenter = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        img.setImageResource(R.drawable.ic_launcher);
        img.setLayoutParams(viewParamsCenter);
        rl.add(img);
于 2013-01-29T12:22:30.263 に答える
1

ボタンをクリックすると、画像を動的に設定したいので。画像ビューを線形レイアウトに追加できます

    View LinearLayout1 = findViewById(R.id.Layout1);
    ImageView image1 = new ImageView(getApplicationContext());
    String uri = "@drawable/myresource.png"; // Here you can set the name of
                                                // the image dynamically
    int imageResource = getResources().getIdentifier(uri, null,
            getPackageName());
    Drawable res = getResources().getDrawable(imageResource);
    image1.setImageDrawable(res);
    ((ViewGroup) LinearLayout1).addView(image1);
于 2013-01-29T12:25:32.027 に答える