在 Android 中的 Horizo​​ntalScrollView 中动态插入视图

2023-12-12

我正在为 Android 平板电脑 3.0 开发一个应用程序,该应用程序有一个应可在水平轴上滚动的活动,如电子书。

为此,我在布局上的 Horizo​​ntalScrollView 内使用relativelayout。这是 XML:

<?xml version="1.0" encoding="utf-8"?>

    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="800px"
    android:id="@+id/horizontalScroll"
    android:background="#C6D7D2" android:layout_height="600px">

    <RelativeLayout android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="fill_parent">        
    </RelativeLayout>


</HorizontalScrollView>

该 xml 文件称为 main.xml。

我在java文件中做的是:

setContentView(R.layout.main);
parent = (ViewGroup) findViewById(R.id.container);
View v = createView(); // Thats working for sure.
parent.addView(v);

但它不起作用,视图 V 没有显示在屏幕上。但如果我这样做

addContentView(v)

它将视图 v 添加到屏幕(证明我的方法有效),但它不可滚动,因为它位于 Horizo​​ntalScrollView 之外。我究竟做错了什么?

更新: 我尝试过,但也没有成功:

setContentView(R.layout.main);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ViewGroup parent = (ViewGroup) findViewById(R.id.container);
View v = new View(this);
v.setBackgroundColor(Color.BLUE);
parent.addView(v,params);

我没有蓝色背景。


HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView1);

        LinearLayout topLinearLayout = new LinearLayout(this);
       // topLinearLayout.setLayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT);
        topLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 

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



            final ImageView imageView = new ImageView (this);

            imageView.setTag(i);

            imageView.setImageResource(R.drawable.ic_launcher);

            topLinearLayout.addView(imageView);

            imageView.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    // TODO Auto-generated method stub
                    Log.e("Tag",""+imageView.getTag());
                }
            });


        }

        scrollView.addView(topLinearLayout);


//      ImageView img=(ImageView)findViewById(R.id.imageView1);
//      final ImageLoader imageLoader = ImageLoader.getInstance();
//      
//      
//      
//      ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
//      .threadPoolSize(3)
//      .threadPriority(Thread.NORM_PRIORITY - 2)
//      .memoryCacheSize(1500000) // 1.5 Mb
//      .discCacheSize(50000000) // 50 Mb
//      .httpReadTimeout(10000) // 10 s
//      .denyCacheImageMultipleSizesInMemory()
//      .enableLogging() // Not necessary in common
//      .build();
//  // Initialize ImageLoader with configuration.
//  ImageLoader.getInstance().init(config);
//      
//      final DisplayImageOptions options = new DisplayImageOptions.Builder()
//      .showStubImage(R.drawable.ic_launcher)
//      .cacheInMemory()
//      .cacheOnDisc()
//      .build();
//      
//      imageLoader.displayImage("http://3.bp.blogspot.com/_Sd45ASngYHA/TVC78RORKoI/AAAAAAAAARk/y0GcNkTmb40/s1600/android+logo1.jpg",img,options);
//      
//      img.setOnClickListener(new OnClickListener()
//      {
//          
//          @Overridt
//          public void onClick(View v)
//          {
//              // TODO Auto-generated method stub
//              Dialog d =new Dialog(TestActivity.this);
//              d.setContentView(R.layout.dialog);
//          
//              d.setCancelable(true);
//              
//              ImageView d_img=(ImageView)d.findViewById(R.id.dialog_img);
////                d.setLayoutParams(L)
//              imageLoader.displayImage("http://3.bp.blogspot.com/_Sd45ASngYHA/TVC78RORKoI/AAAAAAAAARk/y0GcNkTmb40/s1600/android+logo1.jpg",d_img,options);
//              
//              d.show();
//              }
//      });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Android 中的 Horizo​​ntalScrollView 中动态插入视图 的相关文章

随机推荐