我正在尝试为我的应用程序创建自定义标题栏。问题是,如果我进行这些更改,它会在启动时崩溃:
this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
问题是什么?我搜索了论坛和网络但找不到答案。感谢。
答案 0 :(得分:0)
在设置标题栏
后尝试调用 setContentView(...)this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
setContentView(R.layout.main);
答案 1 :(得分:0)
这对我有用。在我看来,XML文件中有错误
/** Called when the activity is first created. */
@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.window_main_title);
}
XML layout.window_main_title:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#323331">
<ImageView
android:id="@+id/header"
android:src="@drawable/ic_launcher_32"
android:paddingLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/textHeader"
android:textSize="15dp"
android:textColor="#FEFEFE"
android:text="@string/app_name"
android:paddingLeft="5dp"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>