现在的位置: 首页 > 移动开发 > 正文

android开发关于标题栏的一些改动

2018年09月18日 移动开发 ⁄ 共 1789字 ⁄ 字号 评论关闭

 

在android开发中时常会碰到要全屏显示程序的时候,有2个方法可以解决这个问题

一个是在Activity的setContextView()方法之前调用

//隐藏标题栏  
this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  
//隐藏状态栏  
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  

还有一种是修改AndroidManifest.xml 。

无标题栏-----在application 标签中添加android:theme="@android:style/Theme.NoTitleBar"

无标题栏和状态栏-----application 标签中添加android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

 

自定义标题栏

在AndroidMenifest.xml中

<activity android:name=".FlyWidgetActivity" android:theme="@style/activityTitlebar" >

在titlebar.xml中

<?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="fill_parent"
    android:orientation="horizontal">
    
    <ImageView 
        android:layout_height="20dp"
        android:layout_width="20dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_gravity="center_vertical"
        android:src="@drawable/widgeticon"
        android:id="@+id/titleImageViewId" />
    
    <TextView
        android:text="@string/app_name"
        android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="7pt"
        android:layout_gravity="center_vertical" />
</LinearLayout>

 

在styles.xml中

<?xml version="1.0" encoding="utf-8"?> 

<resources> 

    <style name="activityTitlebar" parent="android:Theme"> 

        <item name="android:windowTitleSize">30dp</item>

        <item name="android:windowTitleBackgroundStyle">@style/titleBackground</item>

    </style>

    <style name="titleBackground">

     <item name="android:background">#DCDCDC</item>

    </style>

</resources> 

 

同时还要在java代码中写上

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);   //标题栏的布局 

就可以改变窗口标题栏的样式。

 

抱歉!评论已关闭.