画面を横方向にスクロールさせたいことも時々あります。縦方向にはScrollViewを使いますが、横方向は HrozontalScrollView を使います。
 
![[Android] HorizontalScrollView 横スクロール 1x1.trans - [Android] HorizontalScrollView 横スクロール](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
Android Studio
2021.2.1
2021.2.1
HorizontalScrollView
例えば、このように横に長い画像を普通に表示させたい場合は、
 
![[Android] HorizontalScrollView 横スクロール 1x1.trans - [Android] HorizontalScrollView 横スクロール](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif) 1600 x 720
1600 x 720
画像をスクリーンサイズに横幅を合わせると見にくくなってしまいます。
 
![[Android] HorizontalScrollView 横スクロール 1x1.trans - [Android] HorizontalScrollView 横スクロール](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
そういう時は横スクロール、HorizontalScrollViewを使って表示させましょう。
レイアウトファイルで記述
HorizontalScrollViewは ScrollView と同じくレイアウトファイルで記述すると簡単です。
 
activity_main.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity">     <ImageView             android:src="@drawable/cat"             android:scaleType="centerCrop"             android:layout_width="wrap_content"             android:layout_height="match_parent"             android:contentDescription="@string/description" /> </HorizontalScrollView> | 
 
strings.xml
| 1 2 3 4 | <resources>     <string name="app_name">YourAppName</string>     <string name="description">cat</string> </resources> | 
 
HorizontalScrollViewの子Viewは1個しか持てません。この場合は横長のImageViewだけなのでこれで済みますが
テキストやボタンなどを入れる場合はLineaLayout等でくくって使用する必要があります。
コードで記述
これをコードだけで記述するとこのようになります。
MainActivity.java
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | //package your.package.name; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.ViewGroup; import android.widget.HorizontalScrollView; import android.widget.ImageView; import android.widget.RelativeLayout; public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState); //        setContentView(R.layout.activity_main);         int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT;         int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;         RelativeLayout layout = new RelativeLayout(this);         layout.setLayoutParams(new RelativeLayout.LayoutParams(                 MATCH_PARENT, MATCH_PARENT));         HorizontalScrollView scrollView = new HorizontalScrollView(this);         scrollView.setLayoutParams(new HorizontalScrollView.LayoutParams(                 MATCH_PARENT, MATCH_PARENT));         ImageView imageView = new ImageView(this);         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cat);         imageView.setImageBitmap(bitmap);         imageView.setLayoutParams(new RelativeLayout.LayoutParams(                 WRAP_CONTENT, WRAP_CONTENT));         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);         // ScrollView に View を追加         scrollView.addView(imageView);         setContentView(scrollView);     } } | 
サンプル動画
関連ページ:
- ScrollView 画面の縦スクロール
- HorizontalScrollView 横スクロール
Reference:
HorizontalScrollView
