【问题标题】:Set Images In HorizontalScrollView programmatically以编程方式在 Horizo​​ntalScrollView 中设置图像
【发布时间】:2018-02-05 05:26:57
【问题描述】:

我以编程方式在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();
                }

【问题讨论】:

    标签: android android-layout horizontalscrollview


    【解决方案1】:

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

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

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

    【讨论】:

    • linearLayout = (LinearLayout) findViewById(R.id.linear); ImageView 图像 = 新的 ImageView(this); int imageResource = getResources().getIdentifier(fileUri.toString(),null,getPackageName());可绘制资源 = getResources().getDrawable(imageResource); image.setImageDrawable(res); linearLayout.addView(image);
    • 我的应用崩溃了
    • int imageResource = getResources().getIdentifier(fileUri.toString(),null,getPackageName());可绘制资源 = getResources().getDrawable(imageResource);
    • 是什么意思?我没有得到你。
    【解决方案2】:

    试试这个

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

    【讨论】:

    • 必须添加单图还是多图?
    • 看看我编辑的答案你必须将你的drawable或设置位图传递给循环内的imageview
    • 我要设置多张图片
    猜你喜欢
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多