Android--自定义标题栏(TopBar)
来源:互联网 发布:河南大学二本专业知乎 编辑:程序博客网 时间:2024/06/04 18:36
使用(以标题属性为例)
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:topbar="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" > <com.*.*.widght.TopBar android:layout_width="match_parent" android:layout_height="wrap_content" topbar:titleBarTitle ="@string/forget_name" > </com.*.*.widght.TopBar></RelativeLayout>
包含status的24dp和标题栏的56dp
dimen
<dimen name="activity_margin_small">4dp</dimen> <dimen name="dimenStatusBarHeight">24dp</dimen> <dimen name="dimenTitleBarHeight">56dp</dimen> <dimen name="dimenTopBarHeight">80dp</dimen> <!--标题栏相关--> <dimen name="dimenTiltBarTitleSize">20sp</dimen> <dimen name="dimenTitleBarImageSize">24dp</dimen> <dimen name="dimenTitleBarImageLayoutSize">48dp</dimen>
TopBar布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="@dimen/dimenTopBarHeight" android:orientation="vertical" tools:ignore ="all" > <View android:layout_width="match_parent" android:layout_height="@dimen/dimenStatusBarHeight" android:background="@color/colorStatusBar" /> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/dimenTitleBarHeight" android:orientation="horizontal" android:background="@color/colorTitleBar" android:padding="@dimen/activity_margin_small" > <RelativeLayout android:id="@+id/left_layout" style="@style/styleTitleBarImageLayout" android:paddingRight="@dimen/activity_margin_small" android:paddingEnd="@dimen/activity_margin_small" > <ImageView android:id="@+id/left_image" style="@style/styleTitleBarImage"/> </RelativeLayout> <TextView android:id="@+id/title" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textColor="@color/colorTextPrimary" android:layout_gravity="center" android:gravity="center" android:textSize="@dimen/dimenTiltBarTitleSize" /> <RelativeLayout android:id="@+id/right_layout" style="@style/styleTitleBarImageLayout" android:paddingLeft="@dimen/activity_margin_small" android:paddingStart="@dimen/activity_margin_small" > <ImageView android:id="@+id/right_image" style="@style/styleTitleBarImage" /> </RelativeLayout> </LinearLayout></LinearLayout>
TopBar自定义属性
<!--自定义标题栏--> <declare-styleable name="TopBar"> <attr name="titleBarTitle" format="string"/> <attr name="titleBarTitleBg" format="reference"/> <attr name="titleBarLeftImage" format="reference"/> <attr name="titleBarRightImage" format="reference"/> <attr name="titleBarBackground" format="reference|color"/> </declare-styleable>
TopBar组件代码
/** * Created by TpOut on 2016/9/28. * Email: 416756910@qq.com */public class TopBar extends LinearLayout{ protected RelativeLayout leftLayout; protected ImageView leftImage; protected RelativeLayout rightLayout; protected ImageView rightImage; protected TextView titleView; protected LinearLayout titleLayout; public TopBar(Context context, AttributeSet attrs, int defStyle) { this(context, attrs); } public TopBar(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public TopBar(Context context) { super(context); init(context, null); } private void init(Context context, AttributeSet attrs){ LayoutInflater.from(context).inflate(R.layout.widght_title_bar, this); leftLayout = (RelativeLayout) findViewById(R.id.left_layout); leftImage = (ImageView) findViewById(R.id.left_image); rightLayout = (RelativeLayout) findViewById(R.id.right_layout); rightImage = (ImageView) findViewById(R.id.right_image); titleView = (TextView) findViewById(R.id.title); titleLayout = (LinearLayout) findViewById(R.id.root); parseStyle(context, attrs); } private void parseStyle(Context context, AttributeSet attrs){ if(attrs != null){ TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar); String title = ta.getString(R.styleable.TopBar_titleBarTitle); if(null != title){ titleView.setText(title); } Drawable titleBgDrawable = ta.getDrawable(R.styleable.TopBar_titleBarTitleBg); if(null != titleBgDrawable){ titleView.setBackground(titleBgDrawable); } Drawable leftDrawable = ta.getDrawable(R.styleable.TopBar_titleBarLeftImage); if (null != leftDrawable) { leftImage.setImageDrawable(leftDrawable); } Drawable rightDrawable = ta.getDrawable(R.styleable.TopBar_titleBarRightImage); if (null != rightDrawable) { rightImage.setImageDrawable(rightDrawable); } Drawable background = ta.getDrawable(R.styleable.TopBar_titleBarBackground); if(null != background) { titleLayout.setBackground(background); } ta.recycle(); } } public void setLeftImageResource(int resId) { leftImage.setImageResource(resId); } public void setRightImageResource(int resId) { rightImage.setImageResource(resId); } public void setLeftLayoutClickListener(View.OnClickListener listener){ leftLayout.setOnClickListener(listener); } public void setRightLayoutClickListener(View.OnClickListener listener){ rightLayout.setOnClickListener(listener); } public void setLeftLayoutVisibility(int visibility){ leftLayout.setVisibility(visibility); } public void setRightLayoutVisibility(int visibility){ rightLayout.setVisibility(visibility); } public void setTitle(String title){ titleView.setText(title); } public void setBackgroundColor(int color){ titleLayout.setBackgroundColor(color); } public RelativeLayout getLeftLayout(){ return leftLayout; } public RelativeLayout getRightLayout(){ return rightLayout; }}
0 0
- Android--自定义标题栏(TopBar)
- Android 自定义标题栏Topbar
- Android 自定义标题栏TopBar
- Android——自定义TopBar(ActionBar)
- Android 自定义View:TopBar
- Android自定义TopBar
- android 自定义Topbar
- 自定义控件---组合控件---标题栏TopBar
- Android UI模板设计---TopBar(标题栏)
- Android UI模板设计---TopBar(标题栏)
- Android Topbar(自定义组合控件)
- android 自定义TopBar的使用
- android-->自定义view,TopBar模板
- Android中自定义Topbar模板
- Android 自定义view实现TopBar
- Android学习(十七)自定义View控件 TopBar
- 自定义View(1)---Topbar
- Android自定义控件1:自定义topBar
- mysql 不是外部内部命令的解决方法(修改环境变量)
- [gtest][001] A quick introduction to the Google C++ Testing Framework
- Fedora安装MysqlWorkbench
- lucene索引源码分析2
- POJ1328 Radar Installation(贪心问题)
- Android--自定义标题栏(TopBar)
- 大二辣
- Android打造ListView和GridView万能适配器
- mysql lock in share mode 和 select for update
- IOS的安全机制
- navicat连接oracle数据库ORA-28547:connection to server failed, probable Oracle Net admin error错误,解决方法
- git 命令大全
- AndroidManifest进修笔记
- 九九乘法表