自定义标题栏

来源:互联网 发布:淘宝无门槛红包设置 编辑:程序博客网 时间:2024/06/01 07:12

1.标题栏需创建一个新的layout(title)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >   <ImageView        android:id="@+id/imageButton1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@android:drawable/ic_input_get"         android:layout_gravity="center_vertical"        />        <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:text="画板"         android:paddingRight="50dp"        android:layout_gravity="center_vertical"        />    </LinearLayout>


2.在activity中调用

    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);               requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);        setContentView(R.layout.activity_main);        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);          

这样就可以了,但可能会触发异常:

Caused by: android.util.AndroidRuntimeException: You cannot combine custom titles with other title features


1.检查一下是否继承的是否为activity

2.,在 app的style中增加下面一个标识

<item name="android:windowActionBar">false</item> 

来去掉action bar. 但是这个方法有个限制 就是这个标识必须是api level 11以上的。
具体如下:

<style name="AppTheme" parent="AppTheme"><item name="android:windowActionBar">false</item> </style>


3.修改一下Andriodmainifest

<application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"         android:label="@string/app_name"        android:theme="@style/AppTheme">        <activity            android:name="com.example.handdraw.HandDraw"            android:theme="@style/AppTheme"           >

应该就可以了。



原创粉丝点击