29

私が使用しているAndroidアプリケーションに取り組んでいるActionBarので、それを開くためのナビゲーションドロワーアイコンと のタイトルがActionBarありActionBarます。タイトルにクリックリスナーを設定してActionBar、新しいものを開始し、Activityナビゲーションドロワーアイコンに別のクリックリスナーを設定して、ナビゲーションドロワーメニューを開きたいです。

ナビゲーション ドロワー アイコンをクリックできましたが、タイトルのActionBarタイトルをクリックすると、ナビゲーション ドロワー メニューが開きます。のタイトルに別のクリック リスナーを設定する方法はありますかActionBar

前もって感謝します。

4

10 に答える 10

24

このコードを onCreate() 関数の下に追加してみてください。これにより、アクション バーのタイトルの下にあるリソースが取得され、OnClickListener を追加するために使用できる ID が割り当てられます。それがどうなるか教えてください!

final int abTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
findViewById(abTitleId).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    //Do something
    }
});
于 2014-07-19T08:33:32.120 に答える
19

タイトルにカスタム レイアウトを使用し、それにリスナーを割り当てることができます。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getActionBar();
    if (actionBar != null) {
        // Disable the default and enable the custom
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        View customView = getLayoutInflater().inflate(R.layout.actionbar_title, null);
        // Get the textview of the title
        TextView customTitle = (TextView) customView.findViewById(R.id.actionbarTitle);


        // Change the font family (optional)
        customTitle.setTypeface(Typeface.MONOSPACE);
        // Set the on click listener for the title
        customTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.w("MainActivity", "ActionBar's title clicked.");
            }
        });
        // Apply the custom view
        actionBar.setCustomView(customView);
    }
}

actionbar_title.xml :

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/actionbarTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="@string/app_name"/>

</LinearLayout>
于 2014-07-19T08:35:42.607 に答える
8

v7:21 をサポートするツールバーを使用している場合。次のコードを確認してください。

Field titleField = Toolbar.class.getDeclaredField("mTitleTextView");
        titleField.setAccessible(true);
        TextView barTitleView = (TextView) titleField.get(mToolbar);
        barTitleView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
于 2015-03-09T03:04:13.457 に答える
7

これは、ツールバーを使用して簡単に行うことができます。以下に示すように、レイアウト xml ファイルでツールバーを定義します。

 <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:background="?colorPrimary"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <TextView
        android:id="@+id/toolbarTitle"
        style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
        android:background="?attr/selectableItemBackground"
        android:layout_width="wrap_content"
        android:gravity="center_vertical"
        android:layout_height="match_parent" />
</android.support.v7.widget.Toolbar>

次に、次のコードを使用して Activity にリスナーを設定できます。

setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

TextView toolbarTitle= (TextView) findViewById(R.id.toolbarTitle);
toolbarTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // DO SOMETHING HERE
        }
    });
于 2015-05-06T12:54:50.180 に答える