我们知道我们创建的每一个Activity,系统默认为我们提供了一下黑色的标题,本篇我将带领大家接触一下如何实现自定义标题样式。相比系统为我们提供的样式,自定义标题可以满足我们唯心所欲的自定义设计,使我们的界面看上去更加的高端上档次,以便更好的吸引用户的使用。下面开始今天的内容介绍:
1、既然是自定义标题样式,首先我们需要设计一个自定义标题布局,通过这个布局文件,我们可以随心所欲的设计我们的标题样式(title.xml):
<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#aa0000"
android:text="这是我的自定义标题"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更多"
/>
</LinearLayout>
2、写好布局文件了,下面我们开始设计标题的样式,项目res目录下styles.xml:
<resources>
<style name="itcastTheme"parent="android:Theme">
<itemname="android:windowContentOverlay">@color/nonecolor</item>
<itemname="android:windowTitleSize">44dp</item><!-- 设置自定义标题的宽度-->
<itemname="android:windowTitleBackgroundStyle">@style/itcastbg</item><!--自定义标题的样式 -->
</style>
<style name="itcastbg">
<itemname="android:background">@drawable/rectangle</item>
</style>
</resources>
3、红色字体部分,是我通过drawable文件下的rectangle.xml文件实现的一个标题背景:
<?xml version="1.0"encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#1DC9CD"
android:startColor="#A2E0FB" />
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
</shape>
4、到这里我么就可以开始修改我们的主Activity:
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title);//设置我们自定义标题
Button mybutton =(Button)findViewById(R.id.button);
mybutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "尽请期待",Toast.LENGTH_SHORT).show();
}
});
}
}
需要注意的是红色部分必须写在引用布局文件之前,不然达不到效果。
5、最后我们需要在AndroidManifest.xml文件中,为我们的Activity设置一下样式:
<?xml version="1.0" encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.hpu.activity_title"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:theme="@style/itcastTheme">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN" />
<categoryandroid:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
好了,关于Android自定义标题的介绍就说完了,当然,开发完APP也是需要进行全方位的检测:www.ineice.com
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。