我試圖實現一個自定義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()必須被調用嗎?
正如提示裡說的,不要在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() );
// ...
}
}