• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > 安卓自定义组合控件--toolbar,安卓控件--toolbar

安卓自定义组合控件--toolbar,安卓控件--toolbar

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了安卓toolbar控件,安卓自定义toolbar,安卓toolbar,安卓toolbar的使用,安卓开发 toolbar等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

安卓自定义组合控件--toolbar,安卓控件--toolbar


最近在学习安卓APP的开发,用到了toolbar这个控件, 最开始使用时include layout这种方法,不过感觉封装性不好,就又改成了自定义组合控件的方式。

 

使用的工具为android studio 2.2,简称AS吧

 

 1.首先创建一个新的自定义控件,如下图。AS会创建3个文件,  一个java文件,一个layout中的xml文件(这个是布局文件),一个values中的xml文件(这个是属性文件)

2. 修改布局文件,代码如下。这里使用了RelativeLayout,  并且宽度和高度都选择了match_parent, 真实的宽度是在调用控件的地方写。

布局很简单, 左边后退按钮(可定义onclick方法),中间标题,右边功能按钮(可隐藏,可更换图标,可定义onclick方法)

 
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_all1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/toolbar_left_button1"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:src="@mipmap/ic_top_back"/>

    <TextView
        android:id="@+id/toolbar_title1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/toolbar_text"
        android:textSize="@dimen/toolbar_text_size"/>

    <ImageView
        android:id="@+id/toolbar_right_button1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"/>

</RelativeLayout>

3. 修改属性文件,这里就定义了一个属性titleText, 用于在布局文件中给toolbar设置title

<resources>
    <declare-styleable name="ToolbarControl">
        <attr name="titleText" format="string"/>
    </declare-styleable>
</resources>

 

4.修改java文件。ToolbarControl类是继承与Toolbar类的。定义了titleStr属性,并且生成getter和setter,这里要和属性文件中定义的属性名一致,类型也要一致,否则会有问题。

还定义了代码设置title,代码设置右边功能菜单的图标、onclick事件。以及左边后退按钮的onclick事件(本来想把后退封装在控件里面,但是没找到好的方法,只能从调用的地方添加个OnclickListenser,  如有方法封装到控件里面,请指教)

 

public class ToolbarControl extends Toolbar {

    private static final String TAG = ToolbarControl.class.getSimpleName();

    private String titleText;

    @BindView(R.id.toolbar_left_button1)
    public ImageView leftButton;
    @BindView(R.id.toolbar_title1)
    public TextView titleTextView;
    @BindView(R.id.toolbar_right_button1)
    public ImageView rightButton;

    public ToolbarControl(Context context) {
        super(context);
        init(context, null);
    }

    public ToolbarControl(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.toolbar_control, this, true);
        ButterKnife.bind(this, view);

        //很重要
        setContentInsetsRelative(0, 0);

        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.ToolbarControl, 0, 0);
        titleText = a.getString(R.styleable.ToolbarControl_titleText);
        Log.d(TAG, titleText);
        titleTextView.setText(titleText);

        a.recycle();
    }

    public void setTitle(String titleStr) {
        if (titleTextView != null) {
            titleTextView.setText(titleStr);
        }
    }

    public void setTitleByResourceId(int rid) {
        if (titleTextView != null) {
            titleTextView.setText(rid);
        }
    }

    public void setRightButtonImage(int resourceId) {
        if (rightButton != null) {
            rightButton.setImageResource(resourceId);
        }
    }

    public void showImage() {
        if (rightButton != null) {
            rightButton.setVisibility(View.VISIBLE);
        }
    }

    public void hideImage() {
        if (rightButton != null) {
            rightButton.setVisibility(View.GONE);
        }
    }

    public void hide() {
        this.setVisibility(View.GONE);
    }

    public void setBackButtonOnClickListerner(OnClickListener listerner) {
        if (leftButton != null && listerner != null) {
            leftButton.setOnClickListener(listerner);
        }
    }

    public void setButtonOnClickListener(OnClickListener listener) {
        if (rightButton != null && listener != null) {
            rightButton.setOnClickListener(listener);
        }
    }

    public String getTitleText() {
        return titleText;
    }

    public void setTitleText(String titleText) {
        this.titleText = titleText;
    }
}

 

5. 布局中引用,  这里要设置控件的颜色、height也设置成wrap_content,并设置minHeight为?attr/actionBarSize

    <com.example.ben.tracktest.controls.ToolbarControl
        android:id="@+id/about_me_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:titleText="@string/about_me_title">

    </com.example.ben.tracktest.controls.ToolbarControl>

 

6. 代码中初始化控件。 首先将空

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 安卓自定义组合控件--toolbar,安卓控件--toolbar

相关文章

  • 2017-05-26git版本控制工具(二)----本地版本库的常用操作,git----
  • 2017-05-26Android HandlerThread 消息循环机制之源码解析
  • 2017-05-26ImageLoader,androidimageloader
  • 2017-05-26retrofit2中ssl的Trust anchor for certification path not found问题,retrofit2anchor
  • 2017-05-26A DB2 Performance Tuning Roadmap--Q-BASED A/A IMPLEMENATION
  • 2017-05-26[Android学习]ListView显示多种item的处理办法
  • 2017-05-26Android Volley框架的使用(4),androidvolley
  • 2017-05-26linux文件系统的规模与瓶颈
  • 2017-05-26Git安装与上传代码至Github,git上传github
  • 2017-05-26Android.mk模板(持续更新中),android.mk更新中

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • android开发零基础入门教程,android入门教程
    • PagerTabStrip在ViewPager的页面中添加标题显示,viewpager添加页面
    • LocationManager使用细节,locationmanager
    • 可伸缩的textview,伸缩textview
    • android 5.0后对于apk 跑32 64 的逻辑
    • Android中使用Handler以及CountDownTimer实现包含倒计时的闪屏页面,countdownview倒计时
    • 2.4.7ListView的焦点问题
    • 编译器开发系列--Ocelot语言5.表达式的有效性检查,--ocelot有效性
    • android:从另外一个activity中返回数据
    • Android内存泄漏排查利器LeakCanary,androidleakcanary

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有