改写Android的header

一、编写header用的xml

  文件代码如下,保存为title.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal"  
    android:layout_width="fill_parent"
    android:background="@drawable/tittle_bg"
    android:layout_height="wrap_content">
               	
  <Button android:id="@+id/preButton"  
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:layout_gravity="center_vertical"
		android:layout_marginLeft="5px"
		android:textSize="18dip"
		android:text="返回"
		android:textColor="#ddd"
		android:paddingLeft="8px"
		android:paddingRight="2px"
		android:background="@drawable/back_button_bg">  
  </Button>
  <TextView 		
		android:id="@+id/titleView"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:layout_weight="1"
		android:textSize="18dip"
		android:textStyle="bold"
		android:textColor="#ddd"
		android:layout_gravity="center"
		android:gravity="center"
		android:paddingLeft="5dip"
		android:paddingRight="5dip"/>
  <Button android:id="@+id/nextButtion"  
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:layout_gravity="center_vertical"
		android:layout_marginRight="5px"
		android:paddingLeft="2px"
		android:paddingRight="2px"
		android:textSize="18dip"
		android:text="首页"
		android:textColor="#ddd"
		android:background="@drawable/next_button_bg">   
  </Button>                 
</LinearLayout> 
	              

              

二、在Activity中添加方法

  在每个要改写headerActivity中(最好是写在基类里,然后每个Activity继承就可以了),onCreate方法里写如下的代码。

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.base);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);


完成前面这两步,图标、文字和背景都有了,但是两边会留有空白。接下来的两步就是为了解决这个问题。

 

三、添加样式文件

  添加如下样式,将其保存为custom_style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <style name="CustomWindowTitleText" parent="android:TextAppearance.WindowTitle">
          <item name="android:textSize">22dip</item>
          <item name="android:textColor">#ddd</item>
          <item name="android:textStyle">bold</item>
          <item name="android:gravity">center_horizontal</item>
        
          <item name="android:paddingRight">8dip</item>
          <item name="android:paddingLeft">8dip</item>
          
     </style>
     <!-- Changes the background color of the title bar -->
     <style name="CustomWindowTitleBackground">
           <item name="android:background">@drawable/nav_bg</item>
     </style>

     <!-- Set the theme for the window title -->
     <!-- NOTE: setting android:textAppearence to style defined above -->
     <style name="CustomWindowTitle" parent="*android:WindowTitle">
          <item name="android:layout_gravity">center</item>
          <item name="android:gravity">center</item>
          <item name="android:textAppearance">@style/CustomWindowTitleText</item>
          <item name="android:shadowDx">0</item>
          <item name="android:shadowDy">0</item>
          <item name="android:shadowRadius">5</item>
          <item name="android:shadowColor">#1155CC</item>
      </style>
      <!-- Override properties in the default theme -->
      <!-- NOTE: you must explicitly the windowTitleSize property, the title bar will not re-size automatically, text will be clipped -->
      <style name="CustomTheme" parent="android:Theme">
           <item name="android:windowTitleSize">36dip</item>
           <item name="android:windowTitleStyle">@style/CustomWindowTitle</item>
           <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
      </style>
      <color name="seg_list_color">#1368ac</color>
</resources>


 


四、在manifest改变系统的theme

  在manifest的Application标签,在Theme属性下选择第三步中的样式文件中所创建的CustomTheme

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpServletResponse是Java Servlet中的一个接口,用于表示对客户端的HTTP响应。为了改写HttpServletResponse,我们可以实现一个自定义的类,该类应该继承HttpServletResponse接口,并重写其中的方法。 对于HttpServletResponse的改写,主要包括以下几个步骤: 1. 创建一个新的类,命名为CustomHttpServletResponse,该类应该实现HttpServletResponse接口。 ```java public class CustomHttpServletResponse implements HttpServletResponse { // 对接口中的方法进行重写 } ``` 2. 在CustomHttpServletResponse中,需要定义一个私有的HttpServletResponse对象,用于保存原始的HttpServletResponse对象,并提供一个构造方法用于传入原始的HttpServletResponse对象,并将其保存在私有变量中。 ```java private HttpServletResponse originalResponse; public CustomHttpServletResponse(HttpServletResponse response) { this.originalResponse = response; } ``` 3. 实现接口中的方法。在CustomHttpServletResponse中,对于每个接口方法,我们需要根据需求进行重写。例如,如果要修改响应的内容类型,可以在setContentType方法中进行修改。 ```java @Override public void setContentType(String type) { // 修改响应的内容类型 String newType = type + ";charset=UTF-8"; originalResponse.setContentType(newType); } ``` 4. 然后,根据需求继续重写其他方法,如setStatus、setHeader等等。 ```java @Override public void setStatus(int sc) { // 修改响应的状态码 originalResponse.setStatus(sc); } @Override public void setHeader(String name, String value) { // 修改响应头部信息 originalResponse.setHeader(name, value); } // 其他方法略 ``` 通过以上步骤,我们就可以成功改写HttpServletResponse。然后,在Servlet中使用CustomHttpServletResponse来替代原始的HttpServletResponse对象,从而实现对响应的定制化操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值