android – 如何使工具栏标题与ActionBar完全相同,而不将其设置为ActionBar?

背景

我希望在PreferenceActivity上显示一个ActionBar,因为它不适用于预蜂窝设备(即使在支持库中).

因为支持库上没有这样的类,所以我不能只调用“setSupportActionBar”.我也不想使用库(如this one),因为我发现的所有库仍然使用Holo样式,所以到目前为止他们还没有得到更新,考虑到这一点.

为此,我试图将ActionBar标题的外观和感觉模仿到支持库v7上的新Toolbar类中.

问题

我已经成功地设置了一个标题一个“向上”按钮,但我无法找到如何使它们像普通的动作栏一样可点击,包括触摸的效果.

代码

这是代码

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>

我试过的

我已经尝试了Toolbar类的所有点击监听器类型,但没有一个帮助.

唯一有效的方法是使用“setNavigationIcon”而不是“setlogo”,但这实际上使得标题和图标变得难以辨认,但只有图标显示其被点击的效果(即使我点击标题).

这个问题

如何添加外观​​&感觉工具栏,有标题&它的标志看起来像ActionBar?

解决方法

请记住,工具栏基本上只是一个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");
        }
    });

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...