`
410063005
  • 浏览: 181166 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

WindowFeature及Activity内置Theme

 
阅读更多

1. 自定义标题栏

1.1 常规自定义标题栏

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

 

自定义Activity的标题栏(Titlebar)

 

http://www.189works.com/article-51509-1.html

 

 

本文仅用到了Window.FEATURE_CUSTOM_TITLE, 

Window还有其他一些feature,比如FEATURE_CONTEXT_MENU,FEATURE_NO_TITLE,FEATURE_LEFT_ICON等。

 

下面是一个来自wfd的完整的例子

wfd中的首页布局文件

 

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical" android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
  
    <ListView android:id="@android:id/list" android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:drawSelectorOnTop="false">  
  
    </ListView>  
    <TextView android:id="@id/android:empty" android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:text="还没有会议刷新看看"  
        android:gravity="center_horizontal" />  
  
    <LinearLayout android:layout_width="fill_parent"  
        android:layout_height="90dp" android:gravity="center_horizontal"  
        android:orientation="vertical" android:layout_alignParentBottom="true">  
        <Button android:text="发起会议" android:id="@+id/launch"  
            android:layout_width="200dp" android:layout_height="wrap_content"></Button>  
  
        <LinearLayout android:layout_width="fill_parent"  
            android:layout_height="wrap_content" android:gravity="center">  
            <CheckBox android:id="@+id/use_password" android:text="" android:layout_width="wrap_content"  
                android:layout_height="wrap_content"></CheckBox>  
            <TextView android:layout_width="wrap_content"  
                android:layout_height="wrap_content" android:text="启用密码"></TextView>  
        </LinearLayout>  
    </LinearLayout>  
  
    <LinearLayout android:id="@+id/home_refresh_bar"  
        android:layout_width="wrap_content" android:layout_height="wrap_content"  
        android:visibility="invisible" android:layout_centerInParent="true"  
        android:gravity="center">  
        <ProgressBar android:layout_width="wrap_content"  
            android:layout_height="wrap_content"></ProgressBar>  
        <TextView android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:text="正在寻找会议室"  
            android:paddingLeft="3dp"></TextView>  
    </LinearLayout>  
      
    <LinearLayout android:id="@+id/home_join_bar"  
        android:layout_width="wrap_content" android:layout_height="wrap_content"  
        android:visibility="invisible" android:layout_centerInParent="true"  
        android:gravity="center">  
        <ProgressBar android:layout_width="wrap_content"  
            android:layout_height="wrap_content"></ProgressBar>  
        <TextView android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:text="正在加入会议室"  
            android:paddingLeft="3dp"></TextView>  
    </LinearLayout>  
</RelativeLayout>  

自定义title的布局

<?xml version="1.0" encoding="utf-8"?>  
  
<!-- 首页title布局 -->  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical" android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
  
    <LinearLayout android:layout_height="fill_parent"  
        android:layout_width="fill_parent" android:orientation="horizontal"  
        android:gravity="center_vertical" android:padding="3dp">  
        <ImageView android:id="@+id/home_app_icon"  
            android:layout_width="wrap_content" android:layout_height="wrap_content"  
            android:src="@drawable/icon" android:paddingRight="5dip"></ImageView>  
  
        <TextView android:id="@+id/home_app_name"  
            android:layout_width="wrap_content" android:layout_height="wrap_content"  
            android:text="@string/app_name"></TextView>  
  
        <LinearLayout android:layout_width="0dip"  
            android:layout_height="wrap_content" android:layout_weight="1"  
            android:gravity="right" android:layout_gravity="center_vertical">  
            <ImageView android:id="@+id/home_refresh"  
                android:layout_width="wrap_content" android:layout_height="wrap_content"  
                android:src="@drawable/search_32" android:paddingRight="15dp"></ImageView>  
            <ImageView android:id="@+id/home_options"  
                android:layout_width="wrap_content" android:layout_height="wrap_content"  
                android:src="@drawable/gear_32"></ImageView>  
        </LinearLayout>  
    </LinearLayout>  
  
    <TextView android:background="@drawable/divider"  
        android:layout_height="1dip" android:layout_width="fill_parent"></TextView>  
</LinearLayout>  

  java代码

 

 

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
    setContentView(R.layout.home);  
    // 设置自定义的title  
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,  
            R.layout.home_custom_title);  
  
    // 添加一个footview  
    // getListView().addFooterView(  
    // LayoutInflater.from(this).inflate(R.layout.home_foot, null));  
  
    mAdapter = new MeetingRoomAdapter();  
    doAddTestData();  
    setListAdapter(mAdapter);  
  
    mRefresh = (ImageView) findViewById(R.id.home_refresh);  
    mOptions = (ImageView) findViewById(R.id.home_options);  
    mRefreshBar = (LinearLayout) findViewById(R.id.home_refresh_bar);  
    mJoinBar = (LinearLayout) findViewById(R.id.home_join_bar);  
  
    mLaunch = (Button) findViewById(R.id.launch);  
    mUsePwd = (CheckBox) findViewById(R.id.use_password);  
  
    mUsePwd.setOnCheckedChangeListener(this);  
    mRefresh.setOnClickListener(this);  
    mOptions.setOnClickListener(this);  
    mLaunch.setOnClickListener(this);  

效果

1.2 PreferenceActivity的自定义标题栏

 

PreferenceActivity 自定义title栏出错

http://www.cnblogs.com/slider/archive/2011/11/11/2245149.html

 

 

开发WFD过程中也遇到这个"PreferenceActivity 自定义title栏出错"的问题。

 

经分析,主要原因是因为PreferenceActivity 跟一般Activity不同. PreferenceActivity 的onCreate方法里面执行了setContentView, 而这个方法必须在

requestWindowFeature之后执行。 反映到我们继承自PreferenceActivity 的具体代码中就是, requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)要在super.onCreate(savedInstanceState)前执行  

代码如下

 

public class SettingsActivity extends PreferenceActivity implements  
        Preference.OnPreferenceChangeListener {  
  
    public static final String KEY_USERNAME = "username";  
  
    private EditTextPreference mUsername;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // 注意, PreferenceActivity  
        // 里面需要把这个放在super.onCreate(savedInstanceState)前面!!!  
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
        super.onCreate(savedInstanceState);  
  
        addPreferencesFromResource(R.xml.settings);  
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,  
                R.layout.settings_custom_title);  
  
        mUsername = (EditTextPreference) findPreference(KEY_USERNAME);  
        mUsername.setSummary(getUsername());  
        mUsername.setText(getUsername());  
  
        mUsername.setOnPreferenceChangeListener(this);  
    }  
  
    @Override  
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,  
            Preference preference) {  
  
        return super.onPreferenceTreeClick(preferenceScreen, preference);  
    }  
  
    private String getUsername() {  
        String name = PrefUtil.get(this, PrefUtil.PREF_SETTINGS, KEY_USERNAME,  
                Build.MODEL);  
        return name;  
    }  
  
    @Override  
    public boolean onPreferenceChange(Preference preference, Object newValue) {  
        if (preference == mUsername) {  
            String val = (String) newValue;  
            Logger.d("val = " + val);  
            if (val != null && !val.equals("") && !val.equals(getUsername())) {  
                mUsername.setSummary(val);  
                PrefUtil.set(this, PrefUtil.PREF_SETTINGS, KEY_USERNAME,  
                        mUsername.getText());  
            }  
        }  
  
        return false;  
    }  
}  
 

2. 标题栏进度条

http://qing.weibo.com/2617185797/9bff160533000f4l.html?retcode=6102

最关键的位置就是在setContentView之前添加下面的这行代码

 

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); //设置为圆形旋转进度条

 

 或者

 

requestWindowFeature(Window.FEATURE_PROGRESS); //设置为水平进度条 注意标题栏的水平进度条最大值是10000,不用自己手动设置
 

 

 

3. Java代码中隐藏标题栏

在Activity.setCurrentView()之前调用此方法

 

private void hideTitle(){
//TODOAuto-generatedmethodstub
requestWindowFeature(Window.FEATURE_NO_TITLE);
}

 

4. Java代码中全屏

在Activity.setCurrentView()之前调用此方法

 

private void hideStatusBar(){
//TODOAuto-generatedmethodstub
// 隐藏标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 定义全屏参数
intflag=WindowManager.LayoutParams.FLAG_FULLSCREEN;
// 获得窗口对象
WindowmyWindow=this.getWindow();
// 设置 Flag标识
myWindow.setFlags(flag,flag);
}

5. manifest中全屏

 

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

 

6. manifest中隐藏标题栏

 

android:theme="@android:style/Theme.NoTitleBar"

 

7. 半透明风格的Activity

 

android:theme="@android:style/Theme.Translucent"

或者先编写一个color.xml

 

<?xmlversion="1.0"encoding="UTF-8"?>
<resources>
<colorname="transparent">#9000</color>
</resources>

再编写一个styles.xml 

 

<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stylename="Transparent">
  <itemname="android:windowBackground">@color/transparent</item>
  <itemname="android:windowIsTranslucent">true</item>
  <itemname="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
</style>
</resources>

 最后使用这个style

 

android:theme="@style/transparent"

 参考http://blog.csdn.net/yuejingjiahong/article/details/6668265

8. 对话框风格的Activity

 

android:theme="@android:style/Theme.Dialog"

9. 运行时切换全屏

 

	/**
	 * 切换全屏和非全屏
	 */
	private void switchFullScreen() {
		if (null != switchFullScreenToast) {
			switchFullScreenToast.cancel();
		}
		
		if (fullScreen) {
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
			switchFullScreenToast = Toast.makeText(this, R.string.exit_full_screen, Toast.LENGTH_SHORT);
			switchFullScreenToast.show();
		} else {
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
			switchFullScreenToast = Toast.makeText(this, R.string.enter_full_screen, Toast.LENGTH_SHORT);
			switchFullScreenToast.show();
		}
		fullScreen = !fullScreen;		
	}

  10. 使用自定义style解决自定义title栏高度太小的问题

 

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
<style name="TransparentListView" parent="@android:style/Widget.ListView">   
  <item name="android:cacheColorHint">@android:color/transparent</item>   
 </style>   
</resources> 

 然后使用这个style

 

<activity android:name=".ui.HomeActivity" android:theme="@style/CmTitleBar"></activity>
 

 

  • 大小: 36.1 KB
分享到:
评论

相关推荐

    Android 关于WebView一些注意点

    Android系统提供了多种内置样式,可以通过设置`android:theme`属性来改变Activity的外观。下面列举了一些常用的样式: - `@android:style/Theme.Dialog`:将Activity显示为对话框模式。 - `@android:style/Theme....

    Android开发之利用Activity实现Dialog对话框

    requestWindowFeature(Window.FEATURE_NO_TITLE); // 去除标题栏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND); // 背景变暗 setContentView...

    透明与窗体的设置

    另外,如果仅需将Activity显示为默认的Dialog,可以简化配置,即只需设置`android:theme="@android:style/Theme.Dialog"`或调用`setTheme(android.R.style.Theme_Dialog)`。 ##### 其他创建Dialog的方法 - 使用`...

    Android 开发常用代码片段

    3. **android:theme属性:** 设置Activity的主题样式,这里使用了系统内置的主题样式`Theme.Black.NoTitleBar.Fullscreen`。 #### 五、设置Activity为Dialog的形式 **知识点概述:** 有时候希望Activity能够以...

    基于MATLAB GUI与CNN的模糊车牌识别系统:从图像预处理到字符识别全流程解析

    内容概要:本文详细介绍了基于MATLAB GUI界面和卷积神经网络(CNN)的模糊车牌识别系统。该系统旨在解决现实中车牌因模糊不清导致识别困难的问题。文中阐述了整个流程的关键步骤,包括图像的模糊还原、灰度化、阈值化、边缘检测、孔洞填充、形态学操作、滤波操作、车牌定位、字符分割以及最终的字符识别。通过使用维纳滤波或最小二乘法约束滤波进行模糊还原,再利用CNN的强大特征提取能力完成字符分类。此外,还特别强调了MATLAB GUI界面的设计,使得用户能直观便捷地操作整个系统。 适合人群:对图像处理和深度学习感兴趣的科研人员、高校学生及从事相关领域的工程师。 使用场景及目标:适用于交通管理、智能停车场等领域,用于提升车牌识别的准确性和效率,特别是在面对模糊车牌时的表现。 其他说明:文中提供了部分关键代码片段作为参考,并对实验结果进行了详细的分析,展示了系统在不同环境下的表现情况及其潜在的应用前景。

    嵌入式八股文面试题库资料知识宝典-计算机专业试题.zip

    嵌入式八股文面试题库资料知识宝典-计算机专业试题.zip

    嵌入式八股文面试题库资料知识宝典-C and C++ normal interview_3.zip

    嵌入式八股文面试题库资料知识宝典-C and C++ normal interview_3.zip

    开关磁阻电机技术参数与建模技术深度解析:4kW电机性能详述

    内容概要:本文深入探讨了一款额定功率为4kW的开关磁阻电机,详细介绍了其性能参数如额定功率、转速、效率、输出转矩和脉动率等。同时,文章还展示了利用RMxprt、Maxwell 2D和3D模型对该电机进行仿真的方法和技术,通过外电路分析进一步研究其电气性能和动态响应特性。最后,文章提供了基于RMxprt模型的MATLAB仿真代码示例,帮助读者理解电机的工作原理及其性能特点。 适合人群:从事电机设计、工业自动化领域的工程师和技术人员,尤其是对开关磁阻电机感兴趣的科研工作者。 使用场景及目标:适用于希望深入了解开关磁阻电机特性和建模技术的研究人员,在新产品开发或现有产品改进时作为参考资料。 其他说明:文中提供的代码示例仅用于演示目的,实际操作时需根据所用软件的具体情况进行适当修改。

    少儿编程scratch项目源代码文件案例素材-剑客冲刺.zip

    少儿编程scratch项目源代码文件案例素材-剑客冲刺.zip

    少儿编程scratch项目源代码文件案例素材-几何冲刺 转瞬即逝.zip

    少儿编程scratch项目源代码文件案例素材-几何冲刺 转瞬即逝.zip

    四象限直流电机速度驱动控制系统PID控制仿真模型设计与实现

    内容概要:本文详细介绍了基于PID控制器的四象限直流电机速度驱动控制系统仿真模型及其永磁直流电机(PMDC)转速控制模型。首先阐述了PID控制器的工作原理,即通过对系统误差的比例、积分和微分运算来调整电机的驱动信号,从而实现转速的精确控制。接着讨论了如何利用PID控制器使有刷PMDC电机在四个象限中精确跟踪参考速度,并展示了仿真模型在应对快速负载扰动时的有效性和稳定性。最后,提供了Simulink仿真模型和详细的Word模型说明文档,帮助读者理解和调整PID控制器参数,以达到最佳控制效果。 适合人群:从事电力电子与电机控制领域的研究人员和技术人员,尤其是对四象限直流电机速度驱动控制系统感兴趣的读者。 使用场景及目标:适用于需要深入了解和掌握四象限直流电机速度驱动控制系统设计与实现的研究人员和技术人员。目标是在实际项目中能够运用PID控制器实现电机转速的精确控制,并提高系统的稳定性和抗干扰能力。 其他说明:文中引用了多篇相关领域的权威文献,确保了理论依据的可靠性和实用性。此外,提供的Simulink模型和Word文档有助于读者更好地理解和实践所介绍的内容。

    嵌入式八股文面试题库资料知识宝典-2013年海康威视校园招聘嵌入式开发笔试题.zip

    嵌入式八股文面试题库资料知识宝典-2013年海康威视校园招聘嵌入式开发笔试题.zip

    少儿编程scratch项目源代码文件案例素材-驾驶通关.zip

    少儿编程scratch项目源代码文件案例素材-驾驶通关.zip

    小区开放对周边道路通行能力影响的研究.pdf

    小区开放对周边道路通行能力影响的研究.pdf

    冷链物流路径优化:基于NSGA-2遗传算法与软硬时间窗策略的研究

    内容概要:本文探讨了冷链物流车辆路径优化问题,特别是如何通过NSGA-2遗传算法和软硬时间窗策略来实现高效、环保和高客户满意度的路径规划。文中介绍了冷链物流的特点及其重要性,提出了软时间窗概念,允许一定的配送时间弹性,同时考虑碳排放成本,以达到绿色物流的目的。此外,还讨论了如何将客户满意度作为路径优化的重要评价标准之一。最后,通过一段简化的Python代码展示了遗传算法的应用。 适合人群:从事物流管理、冷链物流运营的专业人士,以及对遗传算法和路径优化感兴趣的科研人员和技术开发者。 使用场景及目标:适用于冷链物流企业,旨在优化配送路线,降低运营成本,减少碳排放,提升客户满意度。目标是帮助企业实现绿色、高效的物流配送系统。 其他说明:文中提供的代码仅为示意,实际应用需根据具体情况调整参数设置和模型构建。

    少儿编程scratch项目源代码文件案例素材-恐怖矿井.zip

    少儿编程scratch项目源代码文件案例素材-恐怖矿井.zip

    基于STM32F030的无刷电机高压FOC控制方案:滑膜无感FOC技术及保护机制

    内容概要:本文详细介绍了基于STM32F030的无刷电机控制方案,重点在于高压FOC(磁场定向控制)技术和滑膜无感FOC的应用。该方案实现了过载、过欠压、堵转等多种保护机制,并提供了完整的源码、原理图和PCB设计。文中展示了关键代码片段,如滑膜观测器和电流环处理,以及保护机制的具体实现方法。此外,还提到了方案的移植要点和实际测试效果,确保系统的稳定性和高效性。 适合人群:嵌入式系统开发者、电机控制系统工程师、硬件工程师。 使用场景及目标:适用于需要高性能无刷电机控制的应用场景,如工业自动化设备、无人机、电动工具等。目标是提供一种成熟的、经过验证的无刷电机控制方案,帮助开发者快速实现并优化电机控制性能。 其他说明:提供的资料包括详细的原理图、PCB设计文件、源码及测试视频,方便开发者进行学习和应用。

    基于有限体积法Godunov格式的管道泄漏检测模型研究.pdf

    基于有限体积法Godunov格式的管道泄漏检测模型研究.pdf

    嵌入式八股文面试题库资料知识宝典-CC++笔试题-深圳有为(2019.2.28)1.zip

    嵌入式八股文面试题库资料知识宝典-CC++笔试题-深圳有为(2019.2.28)1.zip

    少儿编程scratch项目源代码文件案例素材-几何冲刺 V1.5.zip

    少儿编程scratch项目源代码文件案例素材-几何冲刺 V1.5.zip

Global site tag (gtag.js) - Google Analytics