<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:button="@null"
android:textColor="@color/color"
android:checked="true"
android:layout_marginHorizontal="20dp"
android:layout_height="wrap_content"
android:text="First TextView"
android:clickable="true" />
<RadioButton
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_marginHorizontal="20dp"
android:button="@null"
android:layout_height="wrap_content"
android:text="Second TextView"
android:textColor="@color/color"
android:clickable="true"
android:layout_below="@id/textView1" />
RadioGroup>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textView2" />
LinearLayout>
- 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
创建一个新包 color
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/purple_700" android:state_checked="true"/>
<item android:color="@color/black" android:state_checked="false"/>
selector>
功能页
class MainActivity2 : AppCompatActivity() {
private lateinit var fragmentManager: FragmentManager
private val binding by lazy {
ActivityMain2Binding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
fragmentManager = supportFragmentManager
binding.textView1.setOnClickListener {
replaceFragment(HomeFragment())
}
binding.textView2.setOnClickListener {
replaceFragment(SecondFragment())
}
}
private fun replaceFragment(fragment: Fragment) {
val transaction: FragmentTransaction = fragmentManager.beginTransaction()
transaction.replace(R.id.container, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
}
- 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