투케이2K

565. (android/xml) [레이아웃] HorizontalScrollView 수평 가로 스크롤 뷰 생성 및 특정 좌표 위치로 이동 본문

Android

565. (android/xml) [레이아웃] HorizontalScrollView 수평 가로 스크롤 뷰 생성 및 특정 좌표 위치로 이동

투케이2K 2023. 5. 25. 20:21

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Kotlin

 

[소스 코드]

 

==============================================
[XML 레이아웃 생성]
==============================================

            <!-- [수평 방향의 스크롤뷰] -->
            <HorizontalScrollView
                android:id="@+id/titleScroll"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_gravity="center"
                android:visibility="gone">

                <!-- [LinearLayout : orientation : horizontal 가로] -->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:background="@color/setbg">

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="match_parent"
                        android:text="1"
                        android:gravity="center"
                        android:singleLine="true"
                        android:ellipsize="end"
                        android:textSize="13dp"
                        android:textStyle="bold"
                        android:textColor="#fff"/>

                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="#000"/>

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="match_parent"
                        android:text="2"
                        android:gravity="center"
                        android:singleLine="true"
                        android:ellipsize="end"
                        android:textSize="13dp"
                        android:textStyle="bold"
                        android:textColor="#fff"
                        android:layout_marginLeft="5dp"/>

                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="#000"/>

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="match_parent"
                        android:text="3"
                        android:gravity="center"
                        android:singleLine="true"
                        android:ellipsize="end"
                        android:textSize="13dp"
                        android:textStyle="bold"
                        android:textColor="#fff"
                        android:layout_marginLeft="5dp"/>

                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="#000"/>

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="match_parent"
                        android:text="4"
                        android:gravity="center"
                        android:singleLine="true"
                        android:ellipsize="end"
                        android:textSize="13dp"
                        android:textStyle="bold"
                        android:textColor="#fff"
                        android:layout_marginLeft="5dp"/>

                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="#000"/>

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="match_parent"
                        android:text="5"
                        android:gravity="center"
                        android:singleLine="true"
                        android:ellipsize="end"
                        android:textSize="13dp"
                        android:textStyle="bold"
                        android:textColor="#fff"
                        android:layout_marginLeft="5dp"/>

                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="#000"/>

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="match_parent"
                        android:text="6"
                        android:gravity="center"
                        android:singleLine="true"
                        android:ellipsize="end"
                        android:textSize="13dp"
                        android:textStyle="bold"
                        android:textColor="#fff"
                        android:layout_marginLeft="5dp"/>

                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="#000"/>

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="match_parent"
                        android:text="7"
                        android:gravity="center"
                        android:singleLine="true"
                        android:ellipsize="end"
                        android:textSize="13dp"
                        android:textStyle="bold"
                        android:textColor="#fff"
                        android:layout_marginLeft="5dp"/>

                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="#000"/>

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="match_parent"
                        android:text="8"
                        android:gravity="center"
                        android:singleLine="true"
                        android:ellipsize="end"
                        android:textSize="13dp"
                        android:textStyle="bold"
                        android:textColor="#fff"
                        android:layout_marginLeft="5dp"/>


                </LinearLayout>


            </HorizontalScrollView>







==============================================
[Kotlin 소스 코드]
==============================================

            // [가로 스크롤 이동 x 좌표 값 설정]
            var x_Range = 0


            // [인풋 값 파라미터에 맞게 X 축 좌표값 지정]
            if (count == 1){ 
                x_Range = 250
            }
            else if (count == 2){ 
                x_Range = 450
            }
            else if (count == 3){ 
                x_Range = 650
            }
            else if (count == 4){ 
                x_Range = 850
            }
            else if (count == 5){ 
                x_Range = 1050
            }
            else if (count == 6){ 
                x_Range = 1250
            }
            else if (count == 7){ 
                x_Range = 1450
            }
            else if (count == 8){
                x_Range = 1650
            }


            // [부모 스크롤 뷰 이동 : X / Y 축]
            scrollView.smoothScrollTo(x_Range, 0)

반응형
Comments