日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android开发之最简单的布局点击Tab和Fragment切换源码(特别适合初学者)

發布時間:2023/12/15 Android 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android开发之最简单的布局点击Tab和Fragment切换源码(特别适合初学者) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

老套路看效果圖:

說實話這個布局實現的思路太多了,自己寫以及第三方都特別多,不過對于初學者還是從一點一滴開始學起比較簡單也容易理解。直接上源碼吧,我這個是用的最新的AndroidX庫和Kotlin寫的。

MainActivity.kt

package com.xiayiye.takeout.ui.activityimport android.os.Bundle import android.view.View import android.view.ViewGroup import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import com.xiayiye.takeout.R import com.xiayiye.takeout.ui.fragment.HomeFragment import com.xiayiye.takeout.ui.fragment.MoreFragment import com.xiayiye.takeout.ui.fragment.OrderFragment import com.xiayiye.takeout.ui.fragment.UserFragment import kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() {//添加所有頁面的fragmentval list = listOf<Fragment>(HomeFragment(), OrderFragment(), UserFragment(), MoreFragment())override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//下面是Kotlin語言查找id的方法 // val mainBottomBar = findViewById<LinearLayout>(R.id.main_bottom_bar)initBottomBar()//默認選中第一個changeIndex(0)}private fun initBottomBar() {for (index in 0 until main_bottom_bar.childCount) {main_bottom_bar.getChildAt(index).setOnClickListener(object : View.OnClickListener {override fun onClick(p0: View?) {changeIndex(index)}})}}private fun changeIndex(index: Int) {for (position in 0 until main_bottom_bar.childCount) {val childView = main_bottom_bar.getChildAt(position)if (index == position) {senEnable(childView, false)} else {senEnable(childView, true)}}//點擊哪個切換哪個fragment//Android的app包下 // fragmentManager.beginTransaction().replace(R.id.main_content, list[index])//AndroidX的app包下supportFragmentManager.beginTransaction().replace(R.id.main_content, list[index]).commit()}private fun senEnable(childView: View, isEnabled: Boolean) {childView.isEnabled = isEnabledif (childView is ViewGroup) {for (position in 0 until childView.childCount) {childView.getChildAt(position).isEnabled = isEnabled}}} }

再看下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="match_parent"android:orientation="vertical"tools:context=".ui.activity.MainActivity"><FrameLayoutandroid:id="@+id/main_content"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="@null" /><LinearLayoutandroid:id="@+id/main_bottom_bar"android:layout_width="match_parent"android:layout_height="50dp"android:layout_marginBottom="2dp"android:baselineAligned="false"android:orientation="horizontal"><FrameLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"><ImageViewandroid:layout_width="match_parent"android:layout_height="30dp"android:contentDescription="@string/text_home"android:src="@drawable/main_home_selector" /><TextViewandroid:layout_width="wrap_content"android:layout_height="20dp"android:layout_gravity="bottom|center"android:text="@string/text_home"android:textColor="@drawable/main_bottom_text_color" /></FrameLayout><FrameLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"><ImageViewandroid:layout_width="match_parent"android:layout_height="30dp"android:contentDescription="@string/text_order"android:src="@drawable/main_order_selector" /><TextViewandroid:layout_width="wrap_content"android:layout_height="20dp"android:layout_gravity="bottom|center"android:text="@string/text_order"android:textColor="@drawable/main_bottom_text_color" /></FrameLayout><FrameLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"><ImageViewandroid:layout_width="match_parent"android:layout_height="30dp"android:contentDescription="@string/text_personal"android:src="@drawable/main_me_selector" /><TextViewandroid:layout_width="wrap_content"android:layout_height="20dp"android:layout_gravity="bottom|center"android:text="@string/text_personal"android:textColor="@drawable/main_bottom_text_color" /></FrameLayout><FrameLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"><ImageViewandroid:layout_width="match_parent"android:layout_height="30dp"android:contentDescription="@string/text_more"android:src="@drawable/main_more_selector" /><TextViewandroid:layout_width="wrap_content"android:layout_height="20dp"android:layout_gravity="bottom|center"android:text="@string/text_more"android:textColor="@drawable/main_bottom_text_color" /></FrameLayout></LinearLayout> </LinearLayout>

簡單說下思路:

就是下面放四個FrameLayout。然后每個FrameLayout里面放圖片和文字總共四組,然后分別設置圖片和文字的enable屬性為selector選擇器,設置好了后,在mainactivity里面通過拿到四個FrameLayout的父布局LinearLayout然后遍歷它的子布局,設置每個子布局的點擊事件,點擊哪個子布局切換哪個fragment,然后判斷子布局是否還有子布局,如果有就根據父布局是否可enable設置相應的enable屬性即可。

需要源碼的點擊下載即可:源碼下載

總結

以上是生活随笔為你收集整理的Android开发之最简单的布局点击Tab和Fragment切换源码(特别适合初学者)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。