【问题标题】:how to set custom title bar TextView Value dynamically in android?如何在android中动态设置自定义标题栏TextView值?
【发布时间】:2011-01-25 21:18:32
【问题描述】:

朋友们,

我使用以下 titlebar.xml 文件和代码创建了自定义标题栏

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/myTitle" 
  android:text="This is my new title" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:textColor="@color/titletextcolor"
  android:layout_marginLeft="25px"
   android:paddingTop="3px" 
   /> 

以及在每个活动上显示自定义标题栏的 java 代码。

@Override
    public void onCreate(Bundle savedInstanceState)
    {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);

super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


}

现在我想在每个活动中动态设置 textview 值,谁能指导我如何实现这一点?

在这里使用 findviewbyid 我没有获得该 textview 的引用来设置值,因为 主布局不包含任何具有这样名称但 mytitle 的文本框。

如有任何帮助,将不胜感激。

【问题讨论】:

标签: android layout android-activity


【解决方案1】:

这是设置自定义标题的方式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.main);

    if ( customTitleSupported ) {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
    }

    final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
    if ( myTitleText != null ) {
        myTitleText.setText("========= NEW TITLE ==========");
        myTitleText.setBackgroundColor(Color.GREEN);
    }


}

【讨论】:

  • myTitleText 为空,因为它不在主布局中亲爱的。这是 mytitle 布局。当我们使用 r.id.somthing 时,它会查看用 setContentView(R.layout.main) 编写的布局;就我而言,它不是主要的,而是在其他地方。希望你能理解我的问题。
  • 先测试,后讲。那么,您认为绿色标题来自哪里?
  • 嗨,这正在改变标题栏背景颜色,但我看到两边都有一些偏移。这样看起来不太好。你能帮我解决这个问题吗?
  • 最终 FrameLayout titleContainer = FrameLayout)myTitleText.getParent(); titleContainer.setPadding(0, 0, 0, 0);
  • 当然它不起作用,当您制作自定义布局并寻找 TextView 以在其中设置标题时。您尝试过 dtmilano 吗?顺便说一句,如果它有效,为什么要检查它是否为空?如果是的话会怎样?
【解决方案2】:

SDK 2.1+

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    setTitle("========= NEW TITLE ==========");
}

【讨论】:

  • 这个解决方案太棒了!不知何故,最新的功能都隐藏在一堆随机解决方案下。谢谢:-)
  • 它是否将标题设置在中心?
【解决方案3】:

你试过ActivitysetTitle()方法吗?

【讨论】:

    【解决方案4】:

    my_title.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/header"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:background="#d4e9a9">
    
        <ImageView android:src="@drawable/jetpack"
            android:layout_width="wrap_content" android:layout_alignParentLeft="true"
            android:layout_centerVertical="true" android:id="@+id/back"
            android:layout_height="wrap_content" android:layout_alignParentTop="true" />
    
        <TextView android:id="@+id/title" android:layout_width="wrap_content"
            android:gravity="center_vertical" android:textSize="20px"
            android:textColor="#ffffff" android:layout_alignParentRight="true"
            android:text="New Title" android:background="#a5c639"
            android:layout_height="wrap_content" android:layout_alignParentTop="true"
            android:padding="9dip" android:layout_margin="5dip" />
    </RelativeLayout>
    

    代码:

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);      
    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);      
    
    ((TextView)findViewById(R.id.title)).setText("gradient shadow");
    
    findViewById(R.id.back).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            ((TextView)findViewById(R.id.title)).setText("loce");
        }
    });
    

    因为自定义标题默认是固定的,你应该给自己写一个主题:

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/MyTheme">
    

    【讨论】:

      【解决方案5】:

      尝试以下方法:

      @Override
      public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);        
           requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
           setContentView(R.layout.main);
           getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
           TextView mytitletext = (TextView) findViewById(R.id.myTitle);
           mytitletext.setText("========= NEW TITLE ==========");
      }
      

      您可以使用hierarchyviewer 来查看您的视图是否可访问(您将在层次结构图中看到它的 id)

      【讨论】:

        猜你喜欢
        • 2012-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多