欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程自定义title bar(标题栏)示例

程序员文章站 2024-03-04 15:44:35
本文实例讲述了android编程自定义title bar(标题栏)的方法。分享给大家供大家参考,具体如下: package com.test; import a...

本文实例讲述了android编程自定义title bar(标题栏)的方法。分享给大家供大家参考,具体如下:

package com.test;
import android.app.activity;
import android.content.context;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.drawable.drawable;
import android.os.bundle;
import android.view.view;
import android.view.window;
import android.view.animation.animation;
import android.view.animation.translateanimation;
import android.widget.progressbar;
import android.widget.textview;
public class test extends activity {
  /** called when the activity is first created. */
 boolean customtitlesupported;
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    //check if custom title is supported before setting the content view!
    customtitlesupported = requestwindowfeature(window.feature_custom_title);
    setcontentview(r.layout.main);
   //set custom titlebar
    customtitlebar(gettext(r.string.app_name).tostring(), "hello world!!");
  }
 public void customtitlebar(string left, string right) {
 if (right.length() > 20)
  right = right.substring(0, 20);
 // set up custom title
 if (customtitlesupported) {
  getwindow().setfeatureint(window.feature_custom_title,
   r.layout.title);
  textview titletvleft = (textview) findviewbyid(r.id.titletvleft);
  textview titletvright = (textview) findviewbyid(r.id.titletvright);
  titletvleft.settext(left);
  titletvright.settext(right);
  progressbar titleprogressbar;
  titleprogressbar = (progressbar) findviewbyid(r.id.leadprogressbar);
  // hide the progress bar if it is not needed
  titleprogressbar.setvisibility(progressbar.gone);
 }
 }
}

布局文件: title.xml

<?xml version="1.0" encoding="utf-8"?>
<relativelayout android:id="@+id/relativelayout01"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <textview android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:id="@+id/titletvleft"
 android:text="left"></textview>
 <textview android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:id="@+id/titletvright"
 android:text="right" android:layout_alignparentright="true"></textview>
 <progressbar android:id="@+id/leadprogressbar"
 style="?android:attr/progressbarstylesmall" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:layout_centervertical="true"
 android:layout_torightof="@+id/titletvleft" android:paddingleft="3dip"></progressbar>
</relativelayout>

更多关于android相关内容感兴趣的读者可查看本站专题:《android视图view技巧总结》、《android布局layout技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。