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

分享:Android之自定义标题

2015-11-10 09:52 453 查看
我们知道我们创建的每一个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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: