帮我使用kotlin写出一个中间为突出的圆形图标,并且可以动态添加或减少两边的条目的底部导航栏
时间: 2024-04-10 15:29:58 浏览: 6
以下是使用 Kotlin 在 Android 中创建一个中间为突出的圆形图标,并且可以动态添加或减少两边的条目的底部导航栏的示例代码:
1. 首先,在您的布局文件中添加底部导航栏的代码:
```xml
<LinearLayout
android:id="@+id/bottomNavigationLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/darker_gray">
<!-- 左侧导航项 -->
<TextView
android:id="@+id/leftNavItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Left"
android:gravity="center"
android:textColor="@android:color/white"
android:background="@drawable/bottom_nav_item_background" />
<!-- 中间圆形图标 -->
<ImageView
android:id="@+id/centerNavIcon"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/ic_center_icon"
android:layout_marginTop="-24dp" />
<!-- 右侧导航项 -->
<TextView
android:id="@+id/rightNavItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Right"
android:gravity="center"
android:textColor="@android:color/white"
android:background="@drawable/bottom_nav_item_background" />
</LinearLayout>
```
2. 在 `res/drawable` 目录下创建 `bottom_nav_item_background.xml` 文件,定义底部导航栏项的背景:
```xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/colorAccent" />
<item android:state_focused="true" android:drawable="@color/colorAccent" />
<item android:drawable="@android:color/transparent" />
</selector>
```
3. 在 `res/drawable` 目录下添加 `ic_center_icon.xml` 文件,定义中间圆形图标的资源:
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/colorPrimary" />
</shape>
```
4. 在您的 Activity 或 Fragment 中,使用以下代码来动态添加或减少两边的导航项:
```kotlin
val leftNavItem = findViewById<TextView>(R.id.leftNavItem)
val rightNavItem = findViewById<TextView>(R.id.rightNavItem)
val bottomNavigationLayout = findViewById<LinearLayout>(R.id.bottomNavigationLayout)
// 添加左侧导航项
val newLeftNavItem = TextView(this)
newLeftNavItem.layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
newLeftNavItem.text = "New Left"
newLeftNavItem.gravity = Gravity.CENTER
newLeftNavItem.setTextColor(Color.WHITE)
newLeftNavItem.setBackgroundResource(R.drawable.bottom_nav_item_background)
bottomNavigationLayout.addView(newLeftNavItem, 0)
// 添加右侧导航项
val newRightNavItem = TextView(this)
newRightNavItem.layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
newRightNavItem.text = "New Right"
newRightNavItem.gravity = Gravity.CENTER
newRightNavItem.setTextColor(Color.WHITE)
newRightNavItem.setBackgroundResource(R.drawable.bottom_nav_item_background)
bottomNavigationLayout.addView(newRightNavItem)
// 移除左侧导航项
bottomNavigationLayout.removeView(leftNavItem)
// 移除右侧导航项
bottomNavigationLayout.removeView(rightNavItem)
```
注意,上述代码中的 `R.id.leftNavItem`、`R.id.rightNavItem` 和 `R.id.bottomNavigationLayout` 分别是对应布局文件中导航项和底部导航栏的视图的 ID。
这样,您就可以通过动态添加或减少两边的导航项来实现底部导航栏的需求,并且使用中间的圆形图标突出显示。您可以根据自己的需求进行样式和布局的调整。