分享:Android之自定义标题

我们知道我们创建的每一个Activity,系统默认为我们提供了一下黑色的标题,本篇我将带领大家接触一下如何实现自定义标题样式。相比系统为我们提供的样式,自定义标题可以满足我们唯心所欲的自定义设计,使我们的界面看上去更加的高端上档次,以便更好的吸引用户的使用。下面开始今天的内容介绍:

 

1、既然是自定义标题样式,首先我们需要设计一个自定义标题布局,通过这个布局文件,我们可以随心所欲的设计我们的标题样式(title.xml):

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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">
         <item name="android:windowContentOverlay">@color/nonecolor</item>
         <item name="android:windowTitleSize">44dp</item><!-- 设置自定义标题的宽度 -->
         <item name="android:windowTitleBackgroundStyle">@style/itcastbg</item><!-- 自定义标题的样式 -->
    </style>
   
    <style name="itcastbg">
        <item name="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"?>
<manifest xmlns: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>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

 

好了,关于Android自定义标题的介绍就说完了,当然,开发完APP也是需要进行全方位的检测:www.ineice.com


转载于:https://my.oschina.net/u/2455226/blog/528348

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值