简体   繁体   English

在Horizo​​ntalScrollView中以编程方式设置图像

[英]Programmatically set images in HorizontalScrollView

I am creating android app with Programmatically set images in HorizontalScrollView and click image in HorizontalScrollView that image display in large Image view don't know how to create it help me to complete my code 我正在通过在Horizo​​ntalScrollView中以编程方式设置图像来创建Android应用程序,然后在Horizo​​ntalScrollView中单击图像,以大图像视图显示的图像不知道如何创建它有助于我完成代码

<HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/horizontal">

        <LinearLayout
            android:id="@+id/linear"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
        </LinearLayout>
    </HorizontalScrollView>
    <ImageView
        android:layout_marginTop="60dp"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

Here Mainactivity 这里主要活动

public class MainActivity extends AppCompatActivity {

    private static final int RESULT_LOAD_IMAGE = 1;
    private TextView deis;
    private Button choose;
    private LayoutInflater mInflater;
    private LinearLayout mlinear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        deis=(TextView)findViewById(R.id.textview);
        choose=(Button)findViewById(R.id.button);
        mInflater = LayoutInflater.from(this);

        choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Picture"), RESULT_LOAD_IMAGE);

            }
        });


    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
            if(data.getClipData() != null){
                int totalItemsSelected = data.getClipData().getItemCount();

                for(int i = 0; i < totalItemsSelected; i++){

                    Uri fileUri = data.getClipData().getItemAt(i).getUri();
                    mlinear=(LinearLayout)findViewById(R.id.linear);




                }
                //Toast.makeText(MainActivity.this, "Selected Multiple Files"+fileUri, Toast.LENGTH_SHORT).show();
            }

Complete my code its very helpul for me 完成我的代码对我非常有帮助

add these lines to your code. 将这些行添加到您的代码中。

LinearLayout linearLayout = findViewById(R.id.linear);
1-create an instance of ImageView
ImageView image = new ImageView(this);
//find resource from uril
int imageResource = 
getResources().getIdentifier(fileUri,null,getPackageName());
Drawable res = getResources().getDrawable(imageResource);
image.setImageDrawable(res);
//this will add the imageto the layout.
linearLayout.addView(image);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM