TextView中的操作栏 [英] TextView on Action Bar

查看:125
本文介绍了TextView中的操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个TextView的动作条,我可以动态地修改。 为什么低于code不工作????

 < XML版本=1.0编码=UTF-8&GT?;
    < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@机器人:ID / text1中
        风格=@风格/ TextMedium
        机器人:以下属性来=5DP
        机器人:单线=真
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:ellipsize =金字招牌/>
 

在活动的onCreate方法,

 查看addView = getLayoutInflater()膨胀(R.layout.spinner_select_text,空)。
        actionBar.setCustomView(addView,新ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        TextView的标签=(TextView中)addView.findViewById(android.R.id.text1);
        label.setText(你好);
 

解决方案

您还需要 actionBar.setDisplayShowCustomEnabled(真);

仅供参考,你可以给的,而不是自己膨胀它的操作栏布局ID:

  actionBar.setDisplayShowCustomEnabled(真正的);
    actionBar.setCustomView(R.layout.spinner_select_text);

    TextView的标签=(TextView中)actionBar.getCustomView()findViewById(android.R.id.text1)。
 

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

解决方案

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

这篇关于TextView中的操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆