如何在android棒棒糖中获取操作栏标题textview引用
我正在开发演示应用程序,我想在其中应用操作栏标题文本视图上的单击侦听器。我下面的代码在Android 5.0下运行良好,它正确地找到了操作栏标题textview引用,单击listener也运行良好,但在Android棒棒糖上,我在这里得到空值如何在android棒棒糖中获取操作栏标题textview引用,android,android-actionbar,android-5.0-lollipop,android-support-library,android-actionbar-compat,Android,Android Actionbar,Android 5.0 Lollipop,Android Support Library,Android Actionbar Compat,我正在开发演示应用程序,我想在其中应用操作栏标题文本视图上的单击侦听器。我下面的代码在Android 5.0下运行良好,它正确地找到了操作栏标题textview引用,单击listener也运行良好,但在Android棒棒糖上,我在这里得到空值textview abTitleTV=(textview)findViewById(abTitleId) 如何在android棒棒糖中获得动作条标题参考 public class HomeActivity extends ActionBarActivity
textview abTitleTV=(textview)findViewById(abTitleId)代码>
如何在android棒棒糖中获得动作条标题参考
public class HomeActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// find the view of actionbar title
// set the onclick listener so when you click on it
try {
int abTitleId = 0;
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
abTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
}
else {
// This is the id is from your app's generated R class when ActionBarActivity is used for SupportActionBar
abTitleId = R.id.action_bar_title;
}
TextView abTitleTV = (TextView) findViewById(abTitleId);
abTitleTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(HomeActivity.this, TestActivity.class));
}
});
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
abTitleTV.setHeight(TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics()));
}
abTitleTV.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics()));
// setting the gravity to center
abTitleTV.setGravity(Gravity.CENTER_VERTICAL);
} catch(Exception e) {
Logger.e(TAG, e.getMessage());
}
}
}
你看过这个和解决方法吗?仔细想想,可能没什么帮助。