Android HorizontalScrollView Guide

In Android, you can achieve horizontal scrolling effect by using the HorizontalScrollView widget. Here are the steps to use it:

  1. Add a HorizontalScrollView widget in the XML layout file.
<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!-- 在这里添加需要水平滚动的内容 -->

</HorizontalScrollView>
  1. Add content that needs to scroll horizontally, such as a LinearLayout, to a HorizontalScrollView.
<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <!-- 在这里添加需要水平滚动的子视图 -->

    </LinearLayout>
    
</HorizontalScrollView>
  1. Obtain an instance of the HorizontalScrollView control in the code and add child views to it.
// 获取HorizontalScrollView控件的实例
HorizontalScrollView horizontalScrollView = findViewById(R.id.horizontalScrollView);

// 获取LinearLayout控件的实例
LinearLayout linearLayout = findViewById(R.id.linearLayout);

// 添加子视图到LinearLayout中
for (int i = 0; i < 10; i++) {
    TextView textView = new TextView(this);
    textView.setText("Item " + i);
    linearLayout.addView(textView);
}

This will enable the horizontal scrolling effect. Keep in mind that if the width of the subview exceeds the screen width, users can view all content by scrolling horizontally.

bannerAds