自定义标题栏
来源:互联网 发布:vb编程软件 编辑:程序博客网 时间:2024/05/16 11:57
主界面public class MainActivity extends AppCompatActivity implements MyCustomActionBar.OnIconClickListener { private MyCustomActionBar myCustomActionBar; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myCustomActionBar = (MyCustomActionBar) findViewById(R.id.my_action_bar); myCustomActionBar.SetOnIconClickListener(this); } @Override public void OnIconClick(View icon) { startActivity(new Intent(this,SecondActivity.class)); }}跳转到第二个界面:public class SecondActivity extends AppCompatActivity implements MyCustomActionBar.OnIconClickListener { private MyCustomActionBar myCustomActionBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); myCustomActionBar = (MyCustomActionBar) findViewById(R.id.my_action_bar); myCustomActionBar.SetOnIconClickListener(this); } @Override public void OnIconClick(View icon) { finish(); }}自定义类:public class MyCustomActionBar extends LinearLayout { private RelativeLayout relativeLayout; private TextView biaoti; private ImageView img; private TypedArray typedArray; private int bgcolor; private int textcolor; private float size; private String text; private Drawable drawable; public MyCustomActionBar(Context context) { super(context); initView(context, null); } public MyCustomActionBar(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context, attrs); } private void initView(Context context, AttributeSet attrs) { View inflate = inflate(context, R.layout.my_action_bar_layout, this); relativeLayout = (RelativeLayout) inflate.findViewById(R.id.container); biaoti = (TextView) inflate.findViewById(R.id.biaoti); img = (ImageView) inflate.findViewById(R.id.img); img.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(mOnIconClickListener != null){ mOnIconClickListener.OnIconClick(v); } } }); if (attrs == null) { return; } initAttrs(context,attrs); setViewContent(); } private void initAttrs(Context context, AttributeSet attrs) { if (attrs == null) { return; } typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCustomActionBar); bgcolor = typedArray.getColor(R.styleable.MyCustomActionBar_action_bar_bg_color, Color.GRAY); textcolor = typedArray.getColor(R.styleable.MyCustomActionBar_action_bar_title_text_color, Color.RED); size = typedArray.getDimension(R.styleable.MyCustomActionBar_action_bar_title_text_size, 16); text = typedArray.getString(R.styleable.MyCustomActionBar_action_bar_title_text); drawable = typedArray.getDrawable(R.styleable.MyCustomActionBar_action_bar_icon_src); } public void setViewContent(){ relativeLayout.setBackgroundColor(bgcolor); img.setImageDrawable(drawable); biaoti.setText(text); biaoti.setTextColor(textcolor); biaoti.setTextSize(size); } private OnIconClickListener mOnIconClickListener; public interface OnIconClickListener { void OnIconClick(View icon); } public void SetOnIconClickListener(OnIconClickListener onIconClickListener) { mOnIconClickListener = onIconClickListener; }}attrs:<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyCustomActionBar"> <attr name="action_bar_bg_color" format="color" /> <attr name="action_bar_title_text_color" format="color" /> <attr name="action_bar_title_text_size" format="dimension" /> <attr name="action_bar_title_text" format="string" /> <attr name="action_bar_icon_src" format="reference" /> </declare-styleable></resources>
自定义布局文件:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="80dp" android:background="@color/colorAccent"> <ImageView android:layout_marginLeft="12dp" android:id="@+id/img" android:layout_width="60dp" android:layout_height="60dp" android:layout_centerVertical="true" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/biaoti" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_centerInParent="true" android:layout_centerVertical="true" android:gravity="center" android:src="@mipmap/ic_launcher" android:text="标题" /></RelativeLayout>主界面布局文件:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.fairy.day03.MainActivity"><com.fairy.day03.MyCustomActionBar android:id="@+id/my_action_bar" android:layout_width="match_parent" android:layout_height="80dp" app:action_bar_bg_color="@color/colorAccent" app:action_bar_icon_src="@drawable/brad_pitt" app:action_bar_title_text="MainActivity" app:action_bar_title_text_color="@color/colorPrimaryDark" app:action_bar_title_text_size="16dp" ></com.fairy.day03.MyCustomActionBar></RelativeLayout>
阅读全文
0 0
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义 标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- 自定义标题栏
- Struts2 快速入门
- 测鬼记(上)——大海(六)
- centos7部署禅道和备份
- iOS
- 数据连接池(数据源)的配置方法
- 自定义标题栏
- 用户添加
- jquery-属性操作
- ABAP中USING与CHANGING的用法
- 综合(侧拉+TabLayout+ListView+t跳转+轮播+GridView)
- html5/Css 聊天框素材
- 0921 PL/SQL
- Unity 渲染教程(十四):雾
- centos6.5编译安装mysql5.6