我正在尝试实现一个自定义标题栏:
下面是我的Helper类:
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
发布于 2016-07-07 21:14:01
对于SDK版本23及更高版本,如果您使用AppCompatActivity扩展您的activity,则会抛出相同的RuntimeException。如果您的活动直接从活动派生,则不会发生这种情况。
正如在https://code.google.com/p/android/issues/detail?id=186440中提到的,这是谷歌的一个已知问题
为此提供的解决方法是使用supportRequestWindowFeature()方法,而不是使用requestFeature()。
如果它解决了你的问题,请投上赞成票。
发布于 2018-10-09 20:15:39
在我的例子中,我用Activity
显示了DialogFragment
。在这个对话框片段中,我像在DialogFragment remove black border中一样编写
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NO_FRAME, 0)
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
super.onCreateDialog(savedInstanceState)
val dialog = Dialog(context!!, R.style.ErrorDialogTheme)
val inflater = LayoutInflater.from(context)
val view = inflater.inflate(R.layout.fragment_error_dialog, null, false)
dialog.setTitle(null)
dialog.setCancelable(true)
dialog.setContentView(view)
return dialog
}
删除onCreate()
中的setStyle(STYLE_NO_FRAME, 0)
或更改/删除onCreateDialog
。因为对话框设置将在创建对话框后更改。
发布于 2010-11-23 05:01:17
难道这个错误不能准确地告诉您出了什么问题吗?在调用setContentView
之后调用requestWindowFeature
和setFeatureInt
。
顺便问一下,为什么要调用setContentView
两次?
https://stackoverflow.com/questions/4250149
复制相似问题