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

前端之家收集整理的这篇文章主要介绍了android – 如何使工具栏标题与ActionBar完全相同,而不将其设置为ActionBar?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
背景

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

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

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

问题

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

代码

这是代码

SettingsActivity.java

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

@H_404_18@<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添加到工具栏:

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

听取您的活动中的点击次数

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

猜你在找的Android相关文章