【问题标题】:How to dynamically add Image to dynamically created ImageView如何将图像动态添加到动态创建的 ImageView
【发布时间】:2017-08-17 16:57:28
【问题描述】:

我正在尝试将图像动态添加到动态创建的 ImageView。 一旦我选择图像,应用程序就会崩溃。我用非动态创建的视图进行了尝试,效果很好。我不确定我的问题出在哪里。

我已经膨胀了包含 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>

【问题讨论】:

    标签: android imageview layout-inflater


    【解决方案1】:

    因为您的ImageView 是您动态创建的视图的一部分。所以你必须像这样对它进行参考:

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

    【讨论】:

    • 这有效,但只是第一次。当我尝试添加更多项目时,它不再显示图像。你认为这是为什么?
    • 因为您的布局中只有一个 ImageView。如果您不知道图片的数量,您应该使用ListViewRecyclerView
    • 我确实用 ListView 尝试过,但它只在我设置 ListView 的固定高度时才有效。你会如何解决这样的问题?您认为可能是什么原因造成的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 2013-06-01
    • 1970-01-01
    相关资源
    最近更新 更多