<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>
创建一个新包 color
<?xml version="1.0" encoding="utf-8"?>
<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()
}
}
文章评论