#android #menu #android-toolbar #menuitem #uiswitch
Вопрос:
У меня есть меню из двух пунктов. Один из них-пользовательский переключатель, а другой-значок настроек. Когда я переношу меню на панель инструментов, виден только переключатель. Значки настроек невидимы, но доступны для клика, так как при нажатии на панель инструментов отображается всплывающее сообщение. Вот мой код
Переключатель layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.appcompat.widget.SwitchCompat
android:id="@ id/action_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:thumb="@drawable/thumb"
app:track="@drawable/track" />
</RelativeLayout>
Пользовательский Переключатель
Track.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="rectangle">
<corners android:radius="100dp"/>
<solid android:color="@color/blue"/>
<size
android:width="24dp"
android:height="24dp"
/>
</shape>
</item>
<item android:state_checked="false">
<shape android:shape="rectangle">
<corners android:radius="100dp"/>
<solid android:color="@android:color/darker_gray"/>
<size
android:width="24dp"
android:height="24dp"
/>
</shape>
</item>
</selector>
Переключатель Thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/state_on"/>
<item android:drawable="@drawable/state_off"/>
</selector>
переключатель state_on.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#036998"/>
<size android:width="24dp"
android:height="24dp"/>
</shape>
</item>
</layer-list>
switch state_off.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#201E1E"/>
<size android:width="24dp"
android:height="24dp"/>
</shape>
</item>
</selector>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@ id/state_settings"
android:actionLayout="@layout/user_switch"
android:title=""
app:showAsAction="always" />
<item
android:id="@ id/businessState"
android:checkable="true"
android:enabled="true"
android:icon="@drawable/round_settings"
android:title=""
android:visibility="visible"
android:visible="true"
app:showAsAction="always"
tools:ignore="AlwaysShowAction" />
</menu>
How i inflate the menu in my fragment in my
class AccountInformationFragment : Fragment() {
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_account_information, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val toolbar: Toolbar = view.findViewById(R.id.account_toolbar)
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.homePageFragment,
R.id.searchFragment,
R.id.importantEventsNotificationFragment,
R.id.messageFragment,
R.id.accountInformationFragment
)
)
val navHostFragment = NavHostFragment.findNavController(this)
NavigationUI.setupWithNavController(toolbar, navHostFragment,
appBarConfiguration)
setHasOptionsMenu(true)
(activity as AppCompatActivity).setSupportActionBar(toolbar)
toolbar.setNavigationOnClickListener {
view.findNavController().navigateUp()
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.accounts_menu, menu)
val menuItem: MenuItem = menu.findItem(R.id.state_settings)
menuItem.setActionView(R.layout.user_switch)
val switch: SwitchCompat =
menu.findItem(R.id.state_settings).actionView.findViewById(R.id.action_switch)
switch.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
Toast.makeText(requireActivity(), "Switch Open",
Toast.LENGTH_LONG).show()
} else {
Toast.makeText(requireActivity(), "Switch Closing",
Toast.LENGTH_LONG).show()
}
}
return super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId){
R.id.businessState ->{
Toast.makeText(requireActivity(),"Settings icon
clicked",Toast.LENGTH_LONG).show()
true
}
else -> false
}
}
}
How do I make both of them visible?