我正在尝试在actionBar上添加一个textView,我可以动态编辑它。 为什么以下代码不起作用????
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/TextMedium"
android:paddingLeft="5dp"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
在onCreate活动方法中,
View addView = getLayoutInflater().inflate(R.layout.spinner_select_text, null);
actionBar.setCustomView(addView,new ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView label = (TextView)addView.findViewById(android.R.id.text1);
label.setText("Hello");
答案 0 :(得分:4)
您还需要actionBar.setDisplayShowCustomEnabled(true);
仅供参考,您可以为操作栏指定布局ID,而不是自己充气:
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.spinner_select_text);
TextView label = (TextView) actionBar.getCustomView().findViewById(android.R.id.text1);