现在的位置: 首页 > 综合 > 正文

Activity自定义标题栏

2018年04月27日 ⁄ 综合 ⁄ 共 2278字 ⁄ 字号 评论关闭

Activity的代码:

protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  // 通知系统,使用自定义标题栏
  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

  setContentView(R.layout.main);

  biaoTiLan();// 自定义标题栏

……

}

/** 自定义标题栏 */
 private void biaoTiLan() {
  // 使用自定义标题栏
  getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
    R.layout.zdy_top);
  // 自定义标题栏
  TextView edTitle = (TextView) findViewById(R.id.xsjz_center);
  edTitle.setText("标题栏名称");
  // 返回按钮
  ImageButton btBack = (ImageButton) findViewById(R.id.xsjz_back);
  btBack.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    onBackPressed();
   }
  });
 }

zdy_top.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:paddingTop="5dip"
    android:paddingBottom="5dip"
    android:background="@drawable/titlebar_bg_nor73" >

     <Button
        android:id="@+id/example_right"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@drawable/app_logo" />
   
   
    <ImageButton
        android:id="@+id/xsjz_back"
        android:layout_width="57dp"
        android:layout_height="33dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/xsjz_top_main_back" />

  

    <TextView
        android:id="@+id/xsjz_center"
        android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerHorizontal="true"
     android:layout_centerVertical="true"
     android:textColor="@android:color/white"
     android:gravity="center"
     android:textSize="21sp"
     android:textStyle="bold"
    />

</RelativeLayout>

还有,记得设置Activity的属性: android:theme="@style/myTitle"

 

在styles.xml文件中:(设置高度和背景)

<style name="AllTitleBackground">
        <item name="android:background">#1E90FF</item>
    </style>

 <!-- 自定义标题样式 -->
    <style name="myTitle" parent="android:Theme">
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/AllTitleBackground</item>
    </style>

 

 

 

 

 

 

 

抱歉!评论已关闭.