アクションメニュー項目にトグルボタンがあるアプリがありますが、アクションバーシャーロックを使用していますが、アクションメニュー項目にトグルボタンを配置する方法がわかりません。アクションバーにカスタムレイアウトとして配置したくありませんが、メニュー項目として配置したいと思います。誰かが解決策を見つけたら、私を助けてください。
目的、トグルボタンの状態を変更すると、アルファベットに基づいて、また生年月日に人が並べ替えられます。
前もって感謝します!
アクションメニュー項目にトグルボタンがあるアプリがありますが、アクションバーシャーロックを使用していますが、アクションメニュー項目にトグルボタンを配置する方法がわかりません。アクションバーにカスタムレイアウトとして配置したくありませんが、メニュー項目として配置したいと思います。誰かが解決策を見つけたら、私を助けてください。
目的、トグルボタンの状態を変更すると、アルファベットに基づいて、また生年月日に人が並べ替えられます。
前もって感謝します!
通常のメニューボタンのように追加し、ブール変数で状態を確認するだけで、ソートモードを変更するときにアイコンとタイトルを変更できます
boolean birthSort=false;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_toggle:
if(birthSort){
//change your view and sort it by Alphabet
item.setIcon(icon1)
item.setTitle(title1)
birthSort=false;
}else{
//change your view and sort it by Date of Birth
item.setIcon(icon2)
item.setTitle(title2)
birthSort=true;
}
return true;
}
return super.onOptionsItemSelected(item);
}
android:showAsAction
他のメニューボタンと同じようにxmlに追加し、オーバーフローまたは外部に表示するかどうかを構成することを忘れないでください。
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_toogle"
android:showAsAction="ifRoom"
android:title="Share"
/>
</menu>
他のアプローチは、ActionBarにカスタムレイアウトを使用することです。
基本的に、Toggleを含むレイアウトを定義します。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ToggleButton
android:id="@+id/actionbar_service_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Logging On"
android:textOff="Logging Off" />
</RelativeLayout>
代替案1: 次に、アクティビティまたはフラグメントコンテナで次のことを行います。
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_top);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
...
ToggleButton button = (ToggleButton) findViewById(R.id.actionbar_service_toggle);
実際のToggleButtonがあり、コード内で実際のオブジェクトToggleButtonとして処理していることに注意してください。これには、独自のトグルを再実装する場合に比べて多くの利点があります(テーマ、信頼性、ビュー階層、ネイティブサポートなど)。 。
ソースコードはこちら。
代替案2: それを行う別の方法は、カスタムビューを通常のメニュービューに埋め込むことです。
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="@layout/actionbar_service_toggle" />
</menu>
私のようにactionLayoutが機能しない場合は、代わりに試してください。これapp:actionLayout="@layout/actionbar_service_toggle"
は、
appCompatを使用すると、android名前空間が使用されないためです。android:actionLayout="@layout/actionbar_service_toggle"
app:showAsAction="always"
android:showAsAction="always"
これが最終バージョンです。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ToggleButton
android:id="@+id/actionbar_service_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Logging On"
android:textOff="Logging Off" />
</RelativeLayout>
と
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/actionbar_service_toggle" />
</menu>
メニューにxmlファイルを作成します。
menu.xml
<menu 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" tools:context="si.ziga.switchinsideab.MainActivity">
<item android:id="@+id/switchId" android:title="" android:showasaction="always" android:actionlayout="@layout/switch_layout">
<item android:id="@+id/action_settings" android:orderincategory="100" android:title="@string/action_settings" app:showasaction="never">
</item></item></menu>
次に、レイアウトフォルダーに移動して、新しいxmlファイルを作成し、switch_layout.xmlという名前を付けます。コードは次のとおりです。switch_layout.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="horizontal">
<switch android:id="@+id/switchAB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true">
</switch></relativelayout>
MainActivityクラスで、次のコードをコピーして貼り付けます:
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
switchAB = (Switch)menu.findItem(R.id.switchId)
.getActionView().findViewById(R.id.switchAB);7
または、このことについてこのリンクをたどってください
ここで簡単なこと
<?xml version="1.0" encoding="utf-8"?>
<item
android:title="@string/mode"
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="56dp"
android:layout_marginTop="120dp"
app:showAsAction="ifRoom"
app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
android:checked="false"/>