我希望在PreferenceActivity上显示一个ActionBar,因为它不适用于预蜂窝设备(即使在支持库中)。
因为支持库上没有这样的类,所以我不能只调用" setSupportActionBar" 。我也不想使用图书馆(如this one),因为我发现的所有图书馆仍然使用Holo风格,所以到目前为止它们还没有更新,记住。
为此,我尝试将ActionBar标题的外观和感觉模仿到支持库v7上的新Toolbar类中。
我成功地获得了一个标题和一个" up"按钮,但我无法找到如何使它们像普通操作栏一样可点击,包括触摸的效果。
以下是代码:
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类的所有点击侦听器类型,但没有一个帮助。
唯一有效的方法是使用&#34; setNavigationIcon&#34;而不是&#34; setLogo&#34;,但这实际上使标题和图标变得难以辨认,但只有图标显示其被点击的效果(即使我点击标题)。
如何添加外观&amp;感觉工具栏,有标题和&amp;它的标志看起来像ActionBar?
答案 0 :(得分:24)
我遇到了同样的问题并找到了一种非常方便的方法(对于可点击的标题),使用Toolbar
执行此操作。如果将Toolbar
用作ActionBar
,则无关紧要,它适用于两种情况。
只需在Toolbar
本身设置点击监听器,这将导致完成Toolbar
触发点击监听器,如果不是单击菜单项或主页图标。对我而言,这正是我所期待的......
例如:
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
,主页点击和类似的不会触发onClick
事件。似乎是一个很好的解决方案
答案 1 :(得分:19)
请记住,工具栏基本上只是一个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");
}
});
答案 2 :(得分:0)
实际上,您可以轻松地执行此操作,而无需进行任何变通。您只需循环浏览Toolbar
的所有子视图,找到TextView
即可获得正确的标题文字。
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
}
});
}
}
}
答案 3 :(得分:0)
设置非空工具栏标题后,试试这个设置标题点击监听器
if (toolbarView != null) {
int childCount = toolbarView.getChildCount();
for (int i = 0; i < childCount; i++) {
View view = toolbarView.getChildAt(i);
if (view instanceof TextView) {
view.setOnClickListener(listener);
break;
}
}
}
并在设置非空工具栏字幕后设置字幕点击监听器
if (toolbarView != null) {
int childCount = toolbarView.getChildCount();
for (int i = childCount - 1; i >= 0; i--) {
View view = toolbarView.getChildAt(i);
if (view instanceof TextView) {
view.setOnClickListener(listener);
break;
}
}
}