zoukankan      html  css  js  c++  java
  • 自定义控件之--组合控件(titlebar)

    自定义控件相关知识从郭霖等大神身上学习,这里只不过加上自己的理解和实践,绝非抄袭。
     
    组合控件是自定义控件中最简单的方式,但是是入门自定义控件和进阶的过程:
    那么常见的组合控件有那些?
    比如titlebar和视图中常见的可重用界面布局的可用都可以通过组合控件的方式来进行自定义,并通过向其他类暴露方法和回调来实现对视图内容显示,隐藏,图片展示,动画活动,文字内容的控制。
    废话这么多,写个组合控件来加深影响
    首先思考下,titleBar应该包含那些内容,对了左中右三组控件,分别是左右按钮和中间的标题栏,但是有的页面不现实右边按钮,或者其他控件不显示,那么就会存在视图组件的隐藏和显示的问题。那么还有什么问题,对了,title的颜色或者左右按钮字体的颜色和文字的内容。
    废话真多,你倒是写个组件让我们看看呀。
     
    好吧,撸码走起:
    xml布局文件:
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/rl_top"
          android:layout_width="fill_parent"
          android:layout_height="@dimen/top_height"
          android:background="#f8f8f8" >
          <Button
              android:id="@+id/btn_top_back"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_centerVertical="true"
              android:gravity="center"
              android:background="@drawable/btn_top_back"
              android:textSize="20sp" />
          <TextView
              android:id="@+id/tv_top_title"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true"
              android:gravity="center"
              android:text="@string/app_name"
              android:textColor="@color/black"
              android:textSize="20sp" />
          <Button
              android:id="@+id/btn_top_right"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:layout_alignParentRight="true"
              android:layout_centerVertical="true"
              android:layout_marginRight="@dimen/image_corner_radius"
              android:background="@null"
              android:gravity="center"
              android:textColor="@drawable/btn_top_right_selector"
              android:textSize="16sp" />
          <TextView
              android:layout_width="fill_parent"
              android:layout_alignParentBottom="true"
              android:layout_height="1dp"
              android:background="@color/gray" />
      </RelativeLayout>

    组合控件的写法:在构造方法里面加载布局文件和初始化视图,通过写一些公共的方法控制组件显示和隐藏,以及内容来控制TitleView上面的内容。
    1. public class TitleView extends FrameLayout {
          private Button btn_top_back;
          private TextView tv_top_title;
          private Button btn_top_right;
          public TitleView(Context context, AttributeSet attrs) {
              super(context, attrs);
              LayoutInflater.from(context).inflate(R.layout.common_top, this);
              btn_top_back = (Button) findViewById(R.id.btn_top_back);
              tv_top_title = (TextView) findViewById(R.id.tv_top_title);
              btn_top_right = (Button) findViewById(R.id.btn_top_right);
          }
          public void setvisible(int left,int middle,int right){
              btn_top_back.setVisibility(left);
              tv_top_title.setVisibility(middle);
              btn_top_right.setVisibility(right);
          }
          public void setTitle(String title){
              tv_top_title.setText(title);
          }
          public void setLeftOnclickListener(OnClickListener onClickListener){
              btn_top_back.setOnClickListener(onClickListener);
          }
          public void setRightOnclickListener(OnClickListener onClickListener){
              btn_top_right.setOnClickListener(onClickListener);
          }
          
          public void setRightText(String text){
              btn_top_right.setText(text);
          }
      }

      在布局里面就可以通过包名+类名来重用title布局文件,在activity或者fragement里面初始化,通过TitleView的方法控制内容的显示和隐藏,以及内容的变化,控件事件的响应。

  • 相关阅读:
    路径变量@PathVariable/请求参数@RequestParam的绑定以及@RequestBody
    JSR303后端校验详细笔记
    创建ssm项目步骤
    利用 R 绘制拟合曲线
    在 Linux 中将 Caps 根据是否为修饰键分别映射到 esc 和 Ctrl
    Master Transcription Factors and Mediator Establish Super-Enhancers at Key Cell Identity Genes
    Genomic Evidence for Complex Domestication History of the Cultivated Tomato in Latin America
    Variation Revealed by SNP Genotyping and Morphology Provides Insight into the Origin of the Tomato
    The genetic, developmental, and molecular bases of fruit size and shape variation in tomato
    微信支付jsapi
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/4580523.html
Copyright © 2011-2022 走看看