android自定义标题栏(带进度条)
16lz
2021-01-25
自定义标题栏一共有四步,下面逐步介绍: 一:在strings.xml文件中配置标题栏的背景和大小 [html] view plain copy
二:配置AndroidMainfest.xml文件中相应的activity: [html] view plain copy
三:在layout文件夹中添加自定义标题栏的布局文件:title_bar.xml [html] view plain copy
四:准备就绪,最后在相应的activity中声明使用自定义标题栏,例如MainActivity.java: [java] view plain copy
因为上述例子我用到了圆形的进度条,就简单介绍下它的使用:
进度条布局文件:progress.xml [html] view plain copy
在相应的activity中声明: [java] view plain copy
- <stylename="CustomWindowTitleBackground">
- <!--背景图片-->
- <itemname="android:background">@drawable/home_top</item>
- </style>
- <stylename="title_bar"parent="android:Theme">
- <itemname="android:windowTitleSize">40dp</item>
- <itemname="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
- </style>
二:配置AndroidMainfest.xml文件中相应的activity: [html] view plain copy
- <activityandroid:name=".activity.PlazaActivity">
- <spanstyle="white-space:pre"></span><intent-filter>
- <actionandroid:name="android.intent.action.MAIN"
- android:theme="@style/title_bar"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
三:在layout文件夹中添加自定义标题栏的布局文件:title_bar.xml [html] view plain copy
- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/tvTitle"
- android:layout_width="wrap_content"
- android:layout_height="40dp"
- android:background="@drawable/home_top"
- android:gravity="center"
- android:textColor="#ffffff"
- android:textSize="30sp">
- </TextView>
- <!--圆形进度条-->
- <ProgressBar
- android:id="@+id/progressBar"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_alignParentRight="true"
- android:layout_marginTop="5dp">
- </ProgressBar>
- </RelativeLayout>
四:准备就绪,最后在相应的activity中声明使用自定义标题栏,例如MainActivity.java: [java] view plain copy
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- //声明使用自定义标题栏,注意代码的顺序
- requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
- setContentView(R.layout.plaza);
- getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
- R.layout.title_bar);
- }
因为上述例子我用到了圆形的进度条,就简单介绍下它的使用:
进度条布局文件:progress.xml [html] view plain copy
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <ProgressBarandroid:id="@+id/progress"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- style="?android:attr/progressBarStyleSmallTitle">
- </ProgressBar>
- </LinearLayout>
在相应的activity中声明: [java] view plain copy
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- //声明使用系统默认的圆形进度条。
- requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
- setContentView(R.layout.main);
- getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,R.layout.progress);
- setProgressBarIndeterminateVisibility(true);//开始进度条
- setProgressBarIndeterminateVisibility(false);//结束进度条
- }
更多相关文章
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- dlopen failed: library "/system/lib/libxxx.so" needed or dlo
- Android学习笔记之数据库
- Android读写文件!!!!
- Android中JNI的调用生成头文件步骤
- android下的文件权限