0

我有 2 个 .xml 我有 imageView 的资源,它是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/ic_launcher30005"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="200dp"
        android:layout_height="200dp"

        android:src="@drawable/ic_launcher22" />

    <Button android:id="@+id/bSetWall"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginTop="400dp"
        android:gravity="center"
        android:text="Imposta come sfondo"

        />
</LinearLayout>

在另一个。xml 我有按钮的资源,还有它需要去 imageview 的布局,是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="702dp"
            android:layout_gravity="left"
            android:orientation="vertical" >

    <Button
        android:id="@+id/bopentx"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="tattoo" />

    <Button
        android:id="@+id/bopensax"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="sand" />

     <Button
         android:id="@+id/bopenwx"
         android:layout_width="100dp"
         android:layout_height="100dp"
         android:layout_gravity="left"
         android:text="wet" />

    <Button
        android:id="@+id/bopennx"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="naked" />

    <Button
        android:id="@+id/Bopenlx"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="loincloth" />



    <Button
        android:id="@+id/Bopenbx"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="brazilian" />

     </LinearLayout>
    </ScrollView>

    <ScrollView
        android:id="@+id/Scroll1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right" >

        <LinearLayout
            android:id="@+id/Linear1"
            android:layout_width="wrap_content"
            android:layout_height="702dp"
            android:layout_gravity="right"
            android:orientation="vertical" >

 //the image needs to go here when i click the button


        </LinearLayout>
    </ScrollView>

</LinearLayout>

在菜单类中我使用了这个:公共类菜单扩展了 Activity 实现 View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     Button button1=(Button) findViewById(R.id.bopennx);


    button1.setOnClickListener(new Button.OnClickListener() {







        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            switch (v.getId()){
            case R.id.bopennx:
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(R.layout.activity_main, null);  
                LinearLayout  linearLayout = (LinearLayout) view.findViewById(R.id.Linear1);
                ImageView newImageView=(ImageView)view.findViewById(R.id.imageView1);
                newImageView.setImageResource(R.drawable.ic_brazilian1);
                linearLayout.addView(newImageView);
                break;



            }






        }});
        }
4

1 回答 1

0

为了从 .xml 中获取与您在 contentView 中设置的视图不同的视图(在您的情况下为 R.layout.main),您必须膨胀 xml 并在您膨胀的 xml 中搜索视图.

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.your_xml, null);  
ImageView  imageView = (ImageView) view.findViewById(R.id.imageView1);
于 2013-05-22T14:45:06.863 回答