[英]How to make the Toolbar title clickable exactly like on ActionBar, without setting it as ActionBar?
我希望在 PreferenceActivity 上顯示一個 ActionBar,因為它不適用於蜂窩式設備(即使在支持庫中)。
因為支持庫上沒有這樣的類,所以我不能只調用 "setSupportActionBar" 。 我也不希望使用一個庫(比如這個),因為我發現的所有庫仍然使用 Holo 風格,所以到目前為止它們沒有得到更新,考慮到這一點。
為此,我試圖將 ActionBar 標題的外觀和感覺模仿到新的Toolbar類中,該類出現在支持庫 v7 中。
我已經成功地放置了一個標題和一個“向上”按鈕,但我不知道如何使它們像普通操作欄一樣可點擊,包括觸摸的效果。
這是代碼:
設置Activity.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;
}
}
活動設置.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?
我遇到了同樣的問題,並為它找到了一種非常方便的方法(對於可點擊的標題),使用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
、 home click 和類似的不會觸發onClick
事件。 似乎是一個很好的解決方案
請記住,工具欄基本上只是一個 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");
}
});
實際上,您可以非常輕松地做到這一點,而無需采取任何變通辦法。 您只需要遍歷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
}
});
}
}
}
設置非空工具欄標題后,試試這個設置標題點擊監聽器
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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.