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

时间:2018-02-03 13:37:45

标签: android android-layout horizontalscrollview

我正在创建Android应用程序,在Horizo​​ntalScrollView中以编程方式设置图像,然后单击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" />

此处为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();
            }

完成我的代码,为我提供帮助

1 个答案:

答案 0 :(得分:0)

将这些行添加到您的代码中。

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