android - 如何制作静态导航安卓平板?喜欢这个设计
问题描述
嗨,我在制作这种设计的布局时遇到了麻烦,因为导航栏很难让它发挥作用。
我创建了这样的类:
class MainActivity : AppCompatActivity() {
lateinit var navigationView: NavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigationView = findViewById(R.id.navigation)
navigationView.setNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
private val mOnNavigationItemSelectedListener =
BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_user -> {
val fragment = UserFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_scaner -> {
val fragment = ScannerFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_laporan -> {
val fragment = LaporanFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_notif ->{
val fragment = NotificationFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_chat -> {
val fragment = LaporanFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_profile -> {
val fragment = LaporanFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
}
false
}
private fun addFragment(fragment: Fragment) = supportFragmentManager
.beginTransaction()
.setCustomAnimations(R.anim.design_bottom_sheet_slide_in, R.anim.design_bottom_sheet_slide_out)
.replace(R.id.content, fragment, fragment.javaClass.simpleName)
.commit()
}
这是我的 layout.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:paddingBottom="20dp"
android:background="#FAFAFC"
android:paddingRight="20dp"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearnama"
android:layout_marginStart="20dp"
android:padding="25dp"
android:layout_marginTop="21dp"
android:background="@drawable/bg_white_rounded"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.cardview.widget.CardView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:id="@+id/foto_profil_operator"
android:elevation="10dp"
app:cardCornerRadius="18dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="51dp"
android:layout_height="51dp"
android:scaleType="centerCrop"
android:src="@drawable/lisa" />
</androidx.cardview.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins"
android:layout_marginStart="15dp"
android:layout_weight="0.2"
android:gravity="left"
android:layout_gravity="center"
android:textColor="@color/colorPrimary"
android:textSize="16sp"
android:text="Nama Operator" />
<ImageView
android:layout_width="200dp"
android:layout_height="match_parent"
android:foregroundGravity="right"
android:layout_gravity="end"
android:layout_weight="0"
android:src="@drawable/logo" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearnama"
app:layout_constraintVertical_bias="0.497">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint_menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_weight="0.1"
android:background="@drawable/bg_white_rounded"
android:padding="20dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MENU"
android:fontFamily="@font/poppins"
android:textColor="@color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="@color/white"
app:itemIconTint="@color/black"
app:itemTextColor="@color/black"
app:layout_constraintBottom_toTopOf="@+id/pp2k"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:menu="@menu/navigation">
</com.google.android.material.navigation.NavigationView>
<TextView
android:id="@+id/pp2k"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="navigation"
android:textColor="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_weight="0.5"
android:layout_marginTop="20dp"
android:background="@drawable/bg_white_rounded"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
</FrameLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我已经尝试了几个导航侦听器,例如navigationView.setNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
但它不起作用。也许有人知道我可以如何解决我的问题?提前致谢
解决方案
推荐阅读
- python - 如何将函数用作变量?
- python - 如何从 csv 文件中的数据创建非连续整数的循环?
- html - MacroSyntaxError: TAG 命令格式错误。有人可以帮我弄清楚吗?
- javascript - 如何在 Linux Chrome 中获取键修饰符
- flutter - 如何从列表中获取值
- python - 如何使用 ctypes 将此 Python 对象移动到 C++ 中?
- apache - 如何修复codeigniter3中的其他控制器url
- sql-server - 在存储过程中使用 Like 和运算符
- php - 我试图通过 LARAVEL-MIGRATIONS 在 2 个表之间建立关系,但出现此错误。我该如何解决?
- html - 居中表单和输入