如何使工具栏标题可点击动作条上的完全一样,没有将其设置为动作条? [英] How to make the Toolbar title clickable exactly like on ActionBar, without setting it as ActionBar?

查看:164
本文介绍了如何使工具栏标题可点击动作条上的完全一样,没有将其设置为动作条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示在preferenceActivity的动作条,因为它不适用于pre-蜂窝设备(即使是在支持库)。

I wish to show an ActionBar on PreferenceActivity, as it's not available for pre-Honeycomb devices (even in the support library).

由于这样一类是不提供支持库,我不能只是称之为setSupportActionBar。我也不希望使用的库(如 这个 ),因为所有的我发现还是图书馆的使用全息风格,所以他们没有得到一点到目前为止更新,与此有关。

Because such a class isn't available on the support library, I can't just call "setSupportActionBar" . I also don't wish to use a library (like this one) since all of the libraries I've found still use Holo style, so they didn't get updated so far, with this in mind.

要做到这一点,我试图模仿外观和放大器;在ActionBar标题的手感到新的 工具栏 类,这是presented上支持库V7。

To do this, I'm trying to mimic the look&feel of the title of the ActionBar into the new Toolbar class, that was presented on support library v7 .

我已经成功把一个标题和一个向上按钮,但我不能找出如何让他们可点击喜欢一个正常的操作栏,包括感人的效果。

I've succeeded putting a title and a an "up" button, but I can't find out how to make them clickable like a normal action bar, including the effect of touching.

这里的code:

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>

我已经试过

我已经尝试了所有的听众点击各类工具栏类,但没有帮助。

What I've tried

I've tried all of the click listeners types of the Toolbar class, but none helped.

这几乎工作的唯一的事情就是用setNavigationIcon而不是setLogo,但实际上使标题和放大器;图标是clicakable,但只显示一个图标,它被点击(即使我点击标题效果)。

The only thing that almost worked is to use "setNavigationIcon" instead of "setLogo", but that actually makes the title&icon being clicakable, yet only the icon shows its effect of being clicked (even if I click on the title).

我如何添加外观​​和放大器;感受的工具栏,可将标题和放大器;它的标志看起来像动作条?

How can I add the look & feel of the Toolbar, to have the title & logo of it to look like the ActionBar?

推荐答案

请记住,工具栏基本上是一个这样的ViewGroup你可以添加一个TextView它,听这样的onclick事件。

Just remember that the Toolbar is basically just a ViewGroup so you can add a TextView to it and listen to onClick events like that.

添加到TextView的工具栏在XML:

Add TextView to Toolbar in XML:

<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>

倾听您的活动点击:

Listen for clicks in your Activity:

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

这篇关于如何使工具栏标题可点击动作条上的完全一样,没有将其设置为动作条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆