Fragment是一种可以嵌入到Activity当中的UI片段
可以使用他来为app适配大屏平板
参考资料:《第一行代码》
喜欢就坚持吧!
文章目录
效果
与之前写的界面的区别,就是这个是由一左一右两个Fragment构成的,能更好的的适配平板设备.
具体实现
左Fragment布局left_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/go"/>
</LinearLayout>
右Fragment布局(切换前)right_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#126BE6">
</LinearLayout>
右Fragment布局(切换后)another_right_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E61912">
</LinearLayout>
主布局activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/leftFragment"
android:name="com.example.fragmenttest2.LeftFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/rightLayout"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
逻辑实现MainActivity.kt
package com.example.fragmenttest2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.left_fragment.*
//载入左布局文件
class LeftFragment():Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.left_fragment,container,false)
}
}
//载入右布局文件(切换前)
class RightFragment():Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.right_fragment,container,false)
}
}
//载入右布局文件(切换后)
class AnotherRightFragment():Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.another_right_fragment,container,false)
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
replaceFragment(RightFragment()) //默认情况下加载切换前的右布局文件
button.setOnClickListener{
replaceFragment(AnotherRightFragment()) //点击后动态切换为左布局文件
}
}
private fun replaceFragment(fragment: Fragment){
val fragmentManager = supportFragmentManager //获取FragmentManager
val transaction = fragmentManager.beginTransaction() //开启一个事务
transaction.replace(R.id.rightLayout,fragment) //替换容器内的fragment
transaction.commit() //提交事务
}
}