android 自定义样式文件

来源:互联网 发布:php无限分类处理类 编辑:程序博客网 时间:2024/05/22 11:40

第一步:创建布局文件

 

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="48dp"  
    android:orientation="vertical" >

    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/back"
        android:text=" 返回"
        android:textColor="#858585"
        android:textSize="18dp" />

</LinearLayout>

 

第二步:相关属性配置(String.xml)文件中

<resources>

    <string name="app_name">ezhangkong</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

 
     <style name="CustomWindowTitleBackground">
     <item name="android:background">@drawable/title_style</item>
 </style>
    <style name="titlewindowstyle" parent="android:Theme">
     <item name="android:windowTitleSize">45dp</item>
     <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
 </style>

</resources>

 

 

第三步:activity配置自定义样式文件

public class ZtxxActivity extends Activity {
    //状态消息界面
 @Override
 public void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  //自定义样式文件
  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  //自定义窗口标题的样式 注意顺序
  setContentView(R.layout.activity_ztxx);
  ZtxxActivity.this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title_style);// 注意顺序
 }
 

}

 

 

哦,千万不要忘记在androidManifest.xml文件中配置自定义样式(注意划红线代码)

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/titlewindowstyle">
        <activity
            android:name="com.rf.ezhangkong.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

   >

</application>

 

 

 

 

原创粉丝点击