Android中HorizontalScrollView的使用


     由于移动设备物理显示空间一般有限,不可能一次性的把所有要显示的内容都显示在屏幕上。所以各大平台一般会提供一些可滚动的视图来向用户展示数据。Android平台框架中为我们提供了诸如ListView、GirdView、ScrollView等滚动视图控件,这几个视图控件也是我们平常使用最多的。我下面介绍一下HorizontalScrollView的使用和需要注意的点

     HorizontalScrollView是一个 FrameLayout  , 这意味着你只能在它下面放置一个子控件,这个子控件可以包含很多数据内容。有可能这个子控件本身就是一个布局控件,可以包含非常多的其他用来展示数据的控件。这个布局控件一般使用的是一个水平布局的 LinearLayout   。TextView也是一个可滚动的视图控件,所以一般不需要HorizontalScrollView

     下面介绍一个HorizontalScrollView中包含许多图片,并且可以滚动浏览的示例
        @Override
        protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
             setContentView(R.layout. activity_main);
             
              mLinearLayout = (LinearLayout) findViewById(R.id.mygallery);
             
             File externalDir = Environment. getExternalStorageDirectory();
             String photosPath = externalDir.getAbsolutePath() + "/test/";
             File photosFile = new File(photosPath);
             
              for (File photoFile : photosFile.listFiles()) {
                     mLinearLayout.addView(getImageView(photoFile.getAbsolutePath()));
             }
             
       }

        private View getImageView(String absolutePath) {
             
             Bitmap bitmap = decodeBitmapFromFile(absolutePath, 200, 200);
           LinearLayout layout = new LinearLayout(getApplicationContext());
           layout.setLayoutParams( new LayoutParams(250, 250));
           layout.setGravity(Gravity. CENTER);
           
             ImageView imageView = new ImageView(this);
             imageView.setLayoutParams( new LayoutParams(200,200));
             imageView.setScaleType(ImageView.ScaleType. CENTER_CROP);
             imageView.setImageBitmap(bitmap);
             layout.addView(imageView);
             
              return layout;
       }

        private Bitmap decodeBitmapFromFile(String absolutePath, int reqWidth, int reqHeight) {
           Bitmap bm = null;
           
            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options. inJustDecodeBounds = true ;
            BitmapFactory. decodeFile(absolutePath, options);
           
            // Calculate inSampleSize
            options. inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
           
            // Decode bitmap with inSampleSize set
            options. inJustDecodeBounds = false ;
            bm = BitmapFactory. decodeFile(absolutePath, options);
           
            return bm; 
       }

        private int calculateInSampleSize(Options options, int reqWidth,
                     int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
              
            if (height > reqHeight || width > reqWidth) {
             if (width > height) {
              inSampleSize = Math. round((float)height / ( float)reqHeight);  
             } else {
              inSampleSize = Math. round((float)width / ( float)reqWidth);  
             }  
            }
           
            return inSampleSize; 
       }

     要显示的图片放在外置SDCard中test目录下,上面的示例程序只是显示了一张张大图片的缩略版本,对这方面不懂的可以参看http://blog.csdn.net/tibib/article/details/8726486
      HorizontalScrollView还可以设置滚动到一个指定的位置(x,0),它的子控件也会跟随着滚动。
		
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				// 水平直接滚动800px,如果想效果更平滑可以使用smoothScrollTo(int x, int y)
				hsv.scrollTo(800, 0);
			}
		}, 2000);

效果大致如下


参考文献






  • 33
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
您可以通过两种方式实现这个功能: 1. 使用 `TabLayout` 的 `addOnTabSelectedListener` 方法,监听标签选事件,在选标签后,使用 `HorizontalScrollView` 的 `smoothScrollTo()` 方法将选的标签滚动到居位置。 ```java TabLayout tabLayout = findViewById(R.id.tabLayout); HorizontalScrollView scrollView = findViewById(R.id.scrollView); tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { // 获取选标签的索引 int position = tab.getPosition(); // 获取标签的宽度 int tabWidth = tabLayout.getTabAt(position).getCustomView().getWidth(); // 获取屏幕宽度 int screenWidth = getResources().getDisplayMetrics().widthPixels; // 计算要滚动的距离,使选标签居 int scrollDistance = (tabWidth - screenWidth) / 2; // 滚动到指定位置 scrollView.smoothScrollTo(scrollDistance, 0); } @Override public void onTabUnselected(TabLayout.Tab tab) { // Do nothing } @Override public void onTabReselected(TabLayout.Tab tab) { // Do nothing } }); ``` 2. 自定义 `TabLayout` 的样式,使其支持居显示选标签。您可以使用 `TabGravity.CENTER` 属性将标签居显示。 在 XML 布局文件,将 `TabLayout` 的 `tabGravity` 属性设置为 `center`: ```xml <com.google.android.material.tabs.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="center" /> ``` 请注意,第二种方式只是将选标签居显示,并不会自动滚动到居位置。如果您需要在选标签后进行滚动,仍然需要使用第一种方式的代码。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值