android带返回按钮的自定义标题栏
16lz
2021-01-25
1 定义标题栏样式
<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="TitleBarBackground"> <item name="android:background">@color/style_blue</item> </style> <style name="MyCustomTheme" parent="android:Theme"> <item name="android:windowTitleBackgroundStyle">@style/ TitleBarBackground</item> <item name="android:windowTitleSize">50dp</item> </style> </resources>
2 定义标题栏布局 custom_title.xml
标题栏布局中可以根据自身需求定义。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/head_center_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="" android:textColor="#FFFFFF" /> <TextView android:id="@+id/ head_left_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="" android:textColor="#FFFFFF"/> <Button android:id="@+id/ head_TitleBackBtn" android:layout_width="60dp" android:layout_height="60dp" android:background="@drawable/back" android:gravity="center" android:layout_marginTop="0dp" android:layout_alignParentRight="true"/></RelativeLayout>
3 AndroidManifest.xml中应用自定义标题栏
<application android:name=".IndexActivity" android:icon="@drawable/ic_launchermain" android:label="@string/app_name" android:theme="@style/MyCustomTheme" >
4 自定义代码封装
import android.app.Activity;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.TextView;import com.aaron.util.R;/** * @author aaron * */public class CustomTitleBar {private static Activity mActivity;/** * @see [自定义标题栏] * @param activity * @param title */public static void getTitleBar(Activity activity,String title) {mActivity = activity;activity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);activity.setContentView(R.layout.custom_title);activity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);TextView textView = (TextView) activity.findViewById(R.id.head_center_text);textView.setText(title);Button titleBackBtn = (Button) activity.findViewById(R.id.TitleBackBtn);titleBackBtn.setOnClickListener(new OnClickListener() {public void onClick(View v) {KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK);mActivity.onKeyDown(KeyEvent.KEYCODE_BACK, newEvent);}});}}
5 Activity中添加设置自定义标题代码
@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);// ---------------------------------------------------// 自定义Activity标题栏TitleSet. getTitleBar(this,"我的自定义标题栏");// ---------------------------------------------------setContentView(R.layout.main);}
6 截图
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- android studio 导入 融云问题之一 兼容4.0一下版本
- android Java代码设置textview的字体资源颜色
- Android手机防止休眠
- Android(安卓)input处理机制(二)改键机制
- 12、从头学Android之布局之TableLayout表格布局
- android 默认的几层布局
- [android]使用线控耳机来操控应用功能
- android 从相机或者相册获取照片!