您的位置:首页 > 移动开发 > Android开发

Android 设置全屏及自定义标题栏

2015-07-14 17:16 295 查看
本文主要讲述自定义标题栏,全屏只是引子,而且全屏童鞋们早就知道怎么设置了吧!



一:设置全屏:

1、java代码设置:(注意顺序)

public void setFullscreen() {

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContxt(R.layout.main);

}

2、在AndroidManifest.xml中设置:

如果想整个项目全屏,就在application标签中设置,如果只想某个Activity全屏就在activity标签中设置,加一句:android:theme="@android:style/android.NoTitleBar.Fullscreen";



二:设置(自定义)Activity的标题栏

在应用过程中需要将Activity的标题栏进行修改,一般情况下,通过java代码的setTitle方法可以设置其标题内容。在实际应用过程中可能会有复杂的需求,此时需要进行相关的处理,比如在标题栏上显示两个按钮和一个文本,按钮进行操作可以进行操作。这个时候就需要进行处理,处理方式如下:

1、建立title所需的布局文件:

<LinearLayout

android:id="@+id/LinearLayout01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

xmlns:android="http://schemas.android.com/apk/res/android"

android:gravity="center_horizontal"

>

<Button
android:text="返回"

android:id="@+id/btnBack"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/btn_middle_normal"

android:layout_marginLeft="20dp"

/>

<TextView
android:text="欢迎使用"

android:id="@+id/TextView01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="20dp"

android:typeface="monospace"

android:layout_marginLeft="15dp"

/>

<Button
android:text="下一步"

android:id="@+id/btnNext"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/btn_middle_normal"

android:textSize="15dp"

android:typeface="monospace"

android:layout_marginLeft="15dp"

/>

</LinearLayout>

2、在java代码部分进行设置

protected void onCreate(Bundle
savedInstanceState) {


// TODO Auto-generated method stub


super.onCreate(savedInstanceState);



requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //
注意顺序


setContentView(R.layout.declaration); // 注意顺序



getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
// 注意顺序


R.layout.title);


}

到此步骤可以修改标题栏,但是不能设置标题栏的大小和背景。还需要处理

3、编写style.xml

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

<resources
xmlns:android="http://schemas.android.com/apk/res/android">

<style
name="CustomWindowTitleBackground">


<item
name="android:background">#565656</item>设置背景

</style>

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


<item
name="android:windowTitleSize">50dp</item>
设置标题栏大小


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

</style>

</resources>



4在清单文件中配置


<activity
android:name=".DeclarationActivity"


android:label="@string/app_name"


android:theme="@style/test"


>

这样可以完成配置了。如果想完成更复杂的标题栏,那么需要通过java代码进行自定义。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: