【问题标题】:Trying to change Android title to look better: requestWindowFeature尝试更改 Android 标题以使其看起来更好:requestWindowFeature
【发布时间】:2013-02-04 12:22:18
【问题描述】:

示例代码:

// activity contains different controls so inherits from Activity
public class Main extends Activity implements OnClickListener, TextWatcher  {

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean titleSupported = false;            
        if (true) { // for on/off testing
          titleSupported = this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        } 
        setContentView(R.layout.activity_main);

        // see: http://stackoverflow.com/questions/3438276/change-title-bar-text-in-android
        if (titleSupported)) {
          getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
          final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
          if ( myTitleText != null ) {
            myTitleText.setText("@string/app_name");                    
          }                  
        }

当我包含“requestWindowFeature”时,应用程序在模拟器中停止/崩溃。我不明白为什么。我是 Eclipse 和 Android 的新手,但是从我可以搜索的内容来看,我正在以正确的顺序做事。有什么想法吗?

使用 "requestWindowFeature" before "setContentView" 我得到:

02-04 12:35:05.883: E/AndroidRuntime(755): java.lang.RuntimeException: 无法开始活动 组件信息{com.xxx.yyy/com.xxx.yyy.Main}: android.util.AndroidRuntimeException:您不能组合自定义标题 与其他标题功能

使用 "setContentView" before "requestWindowFeature" 我得到:

02-04 12:32:32.660: E/AndroidRuntime(784): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.xxx.yyy/com.xxx.yyy.Main}: android.util.AndroidRuntimeException:必须调用 requestFeature() 在添加内容之前

【问题讨论】:

  • logcat 上有任何堆栈跟踪吗?
  • 已添加!抱歉,我没有在最初的问题中添加它。
  • 你能告诉我titleSupported是什么吗?
  • 我现在已经包含了我想要实现的完整示例。 (以防万一它以任何方式与我的问题相关和/或其他搜索相同问题的人可以在未来的某一天使用它。)

标签: android


【解决方案1】:

在 setcontentview 之后进行 requestfeature 调用

    setContentView(R.layout.activity_main);
    titleSupported = this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

【讨论】:

  • 我不相信这能解决我的问题。我已经扩展了我原来的帖子。 (根据您的建议,我也遇到了一个错误。)
【解决方案2】:

尝试设置

<item name="android:windowNoTitle">false</item>

在您的主题中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2021-12-30
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多