1 定义继承自android:Theme.Light的style。
<style name="titlebarstyle" parent="android:Theme.Light"> <item name="android:windowTitleSize">50dip</item> </style>
2 android.manifest文件修改theme的主题为定义主题
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/titlebarstyle" >
3 替换布局 代码
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_new_title_bar); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.new_title_bar); }
4 定义新的title bar布局:new_title_bar.xml
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="#FFFFFFFF" android:textSize="14dip" android:paddingTop="1dip" /> <EditText android:id="@+id/searchparameter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ABCDEFGHIJ" android:textSize="14dip" android:layout_margin="1dip" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="OK" android:textSize="14dip" /> </LinearLayout>
其中第1,2步若为4.0以上版本,
则theme必须为android:Theme.Light或继承自android:Theme.Light的style,否则会报错。
(此举意义为不继承API 14中的Theme,用回API 11前的Theme)