Android Application
Kotlin 고급 위젯 - 뷰 컨테이너(View Container) : 스크롤뷰(ScrollView)
작성자 임베디드코리아
작성일 23-09-24 02:02
조회1,650회
댓글0건
------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="150dp"
android:id="@+id/scrollView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:focusable="false" />
</ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
------------------------------------------------------------------------------------
MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var textTV: TextView
private lateinit var btn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textTV = findViewById(R.id.textView)
btn = findViewById(R.id.button)
btn .setOnClickListener {
for (i: Int in 0..100) {
textTV.setText(textTV.getText().toString()+"count"+i+"번\n")
}
}
}
}
**** 이미지 스크롤 뷰 *****************
------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnChange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="이미지 바꾸기"/>
<!--가로 방향은 HorizontalScrollView를 사용한다.-->
<!--가로 방향 스크롤-->
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--세로 방향 스크롤-->
<!--ScrollView의 기본값은 세로 방향-->
<!--ScrollView 내부에 2개 이상의 요소를 배치하면 작동하지 않는다.-->
<!--2개 이상의 요소를 배치할 때는 LinearLayout 등의 다른 레이아웃으로 묶어준다.-->
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/flower01"/>
</ScrollView>
</HorizontalScrollView>
</LinearLayout>
------------------------------------------------------------------------------------
MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var ChangeBTN: Button
private lateinit var ImgView: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ChangeBTN = findViewById(R.id.btnChange)
ImgView = findViewById(R.id.imageView)
ChangeBTN.setOnClickListener {
ImgView.setImageResource(R.drawable.flower02);
}
}
}
158-840 서울시 양천구 남부순환로 571(신월동, 영남타운 307호) l 대표: 박길성 ㅣ Tel:02-2695-1114 ㅣ Fax:02-2695-1113
Copyright © 2015 Embedded Korea. All Rights Reserved.