您的位置:首页 > 移动开发 > Android开发

Android自定义标题栏

2017-02-15 21:00 375 查看
创建一个Android工程,标题栏默认显示的是工程名,下面介绍如何自定义标题栏,我创建工程的MainActivity继承AppCompatActivity,效果图如下



首先在AndroidManifest.xml文件中把默认的标题隐藏,设置

android:theme="@style/Theme.AppCompat.Light.NoActionBar" >


[java] view
plain copy

<?xml version="1.0" encoding="utf-8"?>  

<manifest xmlns:android="http://schemas.android.com/apk/res/android"  

    package="com.nii.orcale" >  

  

    <application  

        android:allowBackup="true"  

        android:icon="@mipmap/ic_launcher"  

        android:label="@string/app_name"  

        android:supportsRtl="true"  

        android:theme="@style/Theme.AppCompat.Light.NoActionBar" >  

        <activity android:name=".MainActivity" >  

            <intent-filter>  

                <action android:name="android.intent.action.MAIN" />  

  

                <category android:name="android.intent.category.LAUNCHER" />  

            </intent-filter>  

        </activity>  

    </application>  

  

</manifest>  

再创建一个标题的布局文件,就叫做title_toolbar.xml,内容如下

[java] view
plain copy

<?xml version="1.0" encoding="utf-8"?>  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

                android:id="@+id/layout_titlebar"  

                android:layout_width="match_parent"  

                android:layout_height="52dp"  

  

                android:background="#ed4255">  

  

    <TextView  

            android:id="@+id/text_title"  

            android:layout_width="match_parent"  

            android:layout_height="match_parent"  

            android:ellipsize="marquee"  

            android:gravity="center_horizontal|center"  

            android:singleLine="true"  

            android:text="@string/title"  

            android:textColor="#ffffffff"  

            android:textSize="20dp"/>  

  

</RelativeLayout>  

最后把这个布局加入到主的布局文件中

<include layout="@layout/title_toolbar"></include>


[html] view
plain copy

<?xml version="1.0" encoding="utf-8"?>  

<LinearLayout  

    xmlns:android="http://schemas.android.com/apk/res/android"  

    xmlns:tools="http://schemas.android.com/tools"  

    android:layout_width="match_parent"  

    android:layout_height="match_parent"  

    android:orientation="vertical"  

    tools:context="com.nii.orcale.MainActivity">  

  

    <include layout="@layout/title_toolbar"></include>  

  

    <ListView  

            android:id="@+id/listView"  

            android:layout_width="match_parent"  

            android:layout_height="wrap_content" >  

    </ListView>  

</LinearLayout>  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: