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

时间:2018-02-05 05:26:58

标签: android android-layout horizontalscrollview

我已在HorizontalScrollView中以编程方式设置了多个图片。当您在HorizontalScrollView中单击该图像时,它会显示一个大图像视图,单击图像将突出显示然后滑动图像。

与图片切换器类似,它会在HorizontalScrollView中突出显示。帮我解决代码,这对我的项目非常有用

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

2 个答案:

答案 0 :(得分:1)

使用RecyclerView代替HorizontalScrollView并设置LinearLayoutManager.HORIZONTAL LinearLayoutManager

recyclerview.setLayoutManager(new LinearLayoutManager(this,
   LinearLayoutManager.HORIZONTAL, false));

点击项目后,使用ViewPager显示具有切换功能的大图像。

答案 1 :(得分:0)

试试这个

LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
    ImageView image = new ImageView(this);
    image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
    image.setMaxHeight(20);
    image.setMaxWidth(20);
    //add your drawable here like this image.setImageResource(R.drawable.redeight)or set like this imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    image.setImageDrawable(drawable);
    // Adds the view to the layout
    layout.addView(image);
}