xiaoyan_12 2012-10-23 03:25 采纳率: 25%
浏览 9107
已采纳

添加content前requestFeature()必须被调用?

我试图实现一个自定义titlebar.
以下是Helper class:

import android.app.Activity;
import android.view.Window;

public class UIHelper {
    public static void setupTitleBar(Activity c) {
        final boolean customTitleSupported = c.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        c.setContentView(R.layout.main);

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

这是在onCreate()中调用的方法:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupUI();
    }

private void setupUI(){
         setContentView(R.layout.main);
         UIHelper.setupTitleBar(this);
    }

错误提醒:

requestFeature() must be called before adding content

在添加content前requestFeature()必须被调用吗?

  • 写回答

4条回答 默认 最新

  • hxn_217 2012-10-26 09:19
    关注

    正如提示里说的,不要在requestFeature()调用setContentView()。
    另一个选择是使用AsyncTask对话框。

    public class CustomDialog extends AlertDialog {
    
       private View content;
    
       public CustomDialog(Context context) {
           super(context);
    
           LayoutInflater li = LayoutInflater.from(context);
           content = inflater.inflate(R.layout.custom_view, null);
    
           setUpAdditionalStuff(); 
           setView(content);           
       }
    
       private void setUpAdditionalStuff();
           // ...
       }
    
       // 在 onPrepareDialog() 方法中调用 ((CustomDialog) dialog).prepare()   
       public void prepare() {
           setTitle(R.string.custom_title);
           setIcon( getIcon() );
           // ...
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug