How to make the Toolbar title clickable exactly like on ActionBar, without setting it as ActionBar?

≯℡__Kan透↙ 提交于 2019-12-18 19:35:27

问题


Background

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

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.

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 .

The problem

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.

The code

Here's the 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.

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

The question

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


回答1:


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.

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");
        }
    });



回答2:


I had the same problem and found a quite convenient way for it (for the clickable title), doing this with the Toolbar. It does not matter, if the Toolbaris used as ActionBar or not, it works for both cases.

Just set the click listener on the Toolbar itself, this will result in the complete Toolbarfiring the click listener, if not a menu item or the home icon is clicked. For me, that's exactly what I expect...

For example:

 setSupportActionBar(toolbar);
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 toolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getThis(), "Toolbar title clicked", Toast.LENGTH_SHORT).show();
        }
    });

onOptionsItemSelected, home click and similar WON'T fire the onClick event. Seems like a good solution




回答3:


You can actually do it pretty easily without having to do any workarounds. You just need to cycle through all the child views of the Toolbar, and find a TextView that's got the correct title text.

String title = toolbar.getTitle();
for(int i = 0; i < toolbar.getChildCount(); i++){
    View tmpView = toolbar.getChildAt(i);
    if("android.support.v7.widget.AppCompatTextView".equals(tmpView.getClass().getName())){
        if(((AppCompatTextView) tmpView).getText().equals(title)){
            tmpView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //do whatever you want here
                }
            });
        }
    }
}


来源:https://stackoverflow.com/questions/26495530/how-to-make-the-toolbar-title-clickable-exactly-like-on-actionbar-without-setti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!