1、建好项目之后在它的layout文件夹下创建一个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=\"match_parent\"
android:text=\"@string/hello_world\"
android:textColor=\"#FF00FF\"
/>
<Button
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:onClick=\"add\"
android:text=\"添加\" />
</LinearLayout>
2、在res/drawable文件下建立rectangle.xml文件,为窗口应用上渐变效果。
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<shape xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:shape=\"rectangle\" >
<!-- 填充色为渐变色,不需要中间颜色startColor开始和结束的颜色.-->
<gradient
android:angle=\"270\"
android:endColor=\"#1DC9CD\"
android:startColor=\"#A2E0FB\"/>
<!-- 定义内间距 -->
<padding
android:left=\"2dp\"
android:top=\"2dp\"
android:right=\"2dp\"
android:bottom=\"2dp\" />
</shape>
3、布局文件:
<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
xmlns:tools=\"http://schemas.android.com/tools\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:paddingBottom=\"@dimen/activity_vertical_margin\"
android:paddingLeft=\"@dimen/activity_horizontal_margin\"
android:paddingRight=\"@dimen/activity_horizontal_margin\"
android:paddingTop=\"@dimen/activity_vertical_margin\"
tools:context=\".MainActivity\" >
<Button
android:id=\"@+id/button1\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:layout_alignParentLeft=\"true\"
android:layout_alignParentTop=\"true\"
android:text=\"Button\" />
</RelativeLayout>
4、通过activity后台代码进行自定义窗口设置。
package com.example.customertitle;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
//自定义标题
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1.设置使用自定义窗口
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
// 2.给窗口引入自定义标题的xml界面文件
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
}
public void add(View v) {
Toast.makeText(this, \"按钮被点击\", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}