代码之家  ›  专栏  ›  技术社区  ›  Sh Ndrukaj

如何将图像动态添加到动态创建的ImageView中

  •  0
  • Sh Ndrukaj  · 技术社区  · 7 年前

    我已经膨胀了包含ImageView的布局,我应该也膨胀ImageView吗?我不确定我的问题在哪里。这是我的主课。

    public class MainActivity extends AppCompatActivity {
    
    int clickCounterIndex = 0;
    LinearLayout picsLayout;
    LayoutInflater inflater;
    View picItem;
    
    Intent intentForPic;
    int RESULT_LOAD_IMAGE = 1;
    ImageView pic;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        intentForPic = new Intent(Intent.ACTION_GET_CONTENT);
        pic = (ImageView) findViewById(R.id.picImageView);
    
        picsLayout = (LinearLayout)findViewById(R.id.picsLayout);
        inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    }
    
    public void addStuff(View view) {
    
        intentForPic = new Intent(Intent.ACTION_GET_CONTENT);
        intentForPic.setType("image/*");
        startActivityForResult(intentForPic, RESULT_LOAD_IMAGE);
    
        picItem = inflater.inflate(R.layout.item_layout, picsLayout, false);
        picsLayout.addView(picItem, clickCounterIndex);
        clickCounterIndex++;
    
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
            Uri imageUri = data.getData();
            pic.setImageURI(imageUri);
    
        }
    }
    }
    

    <LinearLayout android:id="@+id/picsLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="top"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <Button
        android:text="click me!"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="addStuff"/>
    
    </LinearLayout>
    

    动态添加的布局。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    tools:context="com.example.k0k0.thenextsnapchat.imageuploaddemo.MainActivity">
    
    <ImageView
        android:id="@+id/picImageView"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:src="@android:drawable/ic_popup_disk_full"/>
    
    <ProgressBar
        android:id="@+id/picUploadProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:progress="100"/>
    
    <ImageButton
        android:id="@+id/xImageButton"
        android:src="@android:drawable/btn_minus"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="38dp"
        android:layout_marginBottom="38dp"/>
    
    </LinearLayout>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   M.Waqas Pervez    7 年前

    自从你 ImageView 是动态创建的视图的一部分。所以你必须这样引用它:

    picItem = inflater.inflate(R.layout.item_layout, picsLayout, false);
    
    pic = (ImageView) picItem.findViewById(R.id.picImageView);
    
    picsLayout.addView(picItem, clickCounterIndex);
    clickCounterIndex++;