自定义Android应用程序标题条

方法一: 不显示标准的标题条, 完全自定义

步骤1. 在AndroidManifest.xml文件中,  对注册的Activity应用android:theme="@android:style/Theme.NoTitleBar"

示例:

<activity
            android:label="@string/app_name"
            android:name="YourActivityClassName"
            android:theme="@android:style/Theme.NoTitleBar">

</activity>

或者, 在应用程序级别应用android:theme="@android:style/Theme.NoTitleBar" 

示例:

<application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar">

            此处注册Activity

</application>

或者, 在Activity类定义中编程设置不显示标准标题条

示例:

@Override
protected void onCreate(Bundle savedInstanceState) {
            //
            super.onCreate(savedInstanceState);
            // 设置不显示标准标题条
            requestWindowFeature(Window.FEATURE_NO_TITLE);
}

步骤2. 在Layout文件中自定义自己的标题条


方法二: 使用Android的自定义标题条机制

步骤1: 定义自定义标题条布局文件customtitlebar.xml

示例:

<?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/yourLogoImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/menuresource" />
<!-- 标题条标题 -->
    <TextView
        android:id="@+id/yourTitleText"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="Your Title" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:gravity="right"
        android:orientation="horizontal" >
<!-- 右对齐的标题条按钮组 -->
        <ImageButton
            android:id="@+id/yourImageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="@android:color/transparent"
            android:src="@drawable/menumedical" />
    </LinearLayout>

</LinearLayout>

步骤2: 在Activity定义类中编程替换标准标题条

示例:

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     // 请求窗口特征
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
     // 加载主Activity布局
     setContentView(R.layout.main);
     // 设置自定义标题条布局
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custumtitlebar);
     //
     final TextView yourTitleTitle= (TextView) findViewById(R.id.yourTitleTitle);
     if ( yourTitleTitle != null ) {
         yourTitleText.setText("YOUR TITLE");
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值