TextView on Action Bar

怎甘沉沦 提交于 2019-12-06 14:53:03

问题


I am trying to add a textView on the actionBar which I can dynamically edit. Why is the below code not working????

<?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" />

In the onCreate method of activity,

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");

回答1:


You also need actionBar.setDisplayShowCustomEnabled(true);

FYI you can give the action bar your layout id instead of inflating it yourself:

    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setCustomView(R.layout.spinner_select_text);

    TextView label = (TextView) actionBar.getCustomView().findViewById(android.R.id.text1);


来源:https://stackoverflow.com/questions/24712696/textview-on-action-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!