ツールバーのタイトルをActionBarとして設定せずに、ActionBarとまったく同じようにクリック可能にするにはどうすればよいですか?

アンドロイド開発者

バックグラウンド

(サポートライブラリでも)Honeycomb以前のデバイスでは使用できないため、PreferenceActivityにActionBarを表示したいと思います。

このようなクラスはサポートライブラリでは利用できないため、「setSupportActionBar」を呼び出すことはできません。また、私が見つけたすべてのライブラリはまだHoloスタイルを使用しているため、ライブラリ(このような)は使用したくありません。そのため、これを念頭に置いて、これまでのところ更新されていません。

これを行うために、ActionBarのタイトルのルックアンドフィールを、サポートライブラリv7で提示された新しいツールバークラスに模倣しようとしています

問題

タイトルと「上」ボタンの配置に成功しましたが、タッチの効果も含めて、通常のアクションバーのようにクリック可能にする方法がわかりません。

コード

コードは次のとおりです。

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity
  {
  @Override
  protected void onCreate(final Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    addPreferencesFromResource(R.xml.pref_general);
    final Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
    toolbar.setBackgroundColor(getResources().getColor(R.color.windowBackgroundColor));
    toolbar.setTitle("Settings");
    toolbar.setClickable(true);
    toolbar.setLogo(getResIdFromAttribute(this,R.attr.homeAsUpIndicator));
    }

  public static int getResIdFromAttribute(final Activity activity,final int attr)
    {
    if(attr==0)
      return 0;
    final TypedValue typedvalueattr=new TypedValue();
    activity.getTheme().resolveAttribute(attr,typedvalueattr,true);
    return typedvalueattr.resourceId;
    }
  }

activity_settings.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.antonioleiva.materialeverywhere.SettingsActivity" >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/abs__ab_solid_shadow_holo" />

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

</LinearLayout>

私が試したこと

ツールバークラスのすべてのクリックリスナータイプを試しましたが、どれも役に立ちませんでした。

ほとんど機能したのは、「setLogo」の代わりに「setNavigationIcon」を使用することだけですが、実際にはタイトルとアイコンがクリック可能になりますが、アイコンだけがクリックされた効果を示します(タイトルをクリックしても)。

質問

ツールバーのルックアンドフィールを追加して、ツールバーのタイトルとロゴをActionBarのようにするにはどうすればよいですか?

MrEngineer13

ツールバーは基本的に単なるViewGroupであるため、TextViewを追加して、そのようなonClickイベントをリッスンできることを覚えておいてください。

XMLでツールバーにTextViewを追加します。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Settings"
        android:id="@+id/toolbar_title" />

    </android.support.v7.widget.Toolbar>

アクティビティのクリックを聞きます。

toolbarTop.findViewById(R.id.toolbar_title).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG,"Clicked");
        }
    });

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ