Android:设置自定义 Title/Action 栏 - 不起作用
Android: Setting Custom Title/Action Bar - not working
我正在尝试按如下方式设置自定义标题栏(如果重要,请注意此 activity 从 FragmentActivity 扩展):
...
import android.support.v4.app.FragmentActivity;
...
public class MyActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
android.app.ActionBar actionBar = this.getActionBar();
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.window_title, null);
TextView tv = (TextView) v.findViewById(R.id.titlex);
tv.setText("My Title");
actionBar.setCustomView(v);
setContentView(R.layout.myactivity);
}
...
}
但是我根本看不到自定义标题栏,缺少什么?
在actionBar.setCustomView(v);
之前设置actionbar.setDisplayShowCustomEnabled(true);
编辑
这里我传递了创建自定义 ActionBar 的代码
// Action Bar Customization
ActionBar ab =act.getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(act.getResources().getColor(color.ActionBar_bg));
ab.setBackgroundDrawable(colorDrawable);
ab.setDisplayShowTitleEnabled(false); // disables default title on
// actionbar.
ab.setDisplayShowCustomEnabled(true); // enables custom view.
ab.setDisplayShowHomeEnabled(false); // hides app icon.
ab.setTitle("");
// Inflating Layout
LayoutInflater inflater = (LayoutInflater) act.getActionBar()
.getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customActionBar = inflater.inflate(R.layout.actionbar_layout, null);
txtUserName=(TextView)customActionBar.findViewById(R.id.txtUserName);
ab.setCustomView(customActionBar);
我正在尝试按如下方式设置自定义标题栏(如果重要,请注意此 activity 从 FragmentActivity 扩展):
...
import android.support.v4.app.FragmentActivity;
...
public class MyActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
android.app.ActionBar actionBar = this.getActionBar();
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.window_title, null);
TextView tv = (TextView) v.findViewById(R.id.titlex);
tv.setText("My Title");
actionBar.setCustomView(v);
setContentView(R.layout.myactivity);
}
...
}
但是我根本看不到自定义标题栏,缺少什么?
在actionBar.setCustomView(v);
actionbar.setDisplayShowCustomEnabled(true);
编辑
这里我传递了创建自定义 ActionBar 的代码
// Action Bar Customization
ActionBar ab =act.getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(act.getResources().getColor(color.ActionBar_bg));
ab.setBackgroundDrawable(colorDrawable);
ab.setDisplayShowTitleEnabled(false); // disables default title on
// actionbar.
ab.setDisplayShowCustomEnabled(true); // enables custom view.
ab.setDisplayShowHomeEnabled(false); // hides app icon.
ab.setTitle("");
// Inflating Layout
LayoutInflater inflater = (LayoutInflater) act.getActionBar()
.getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customActionBar = inflater.inflate(R.layout.actionbar_layout, null);
txtUserName=(TextView)customActionBar.findViewById(R.id.txtUserName);
ab.setCustomView(customActionBar);