自定义View六

来源:互联网 发布:藤井莉娜开的淘宝店铺 编辑:程序博客网 时间:2024/04/29 05:22

组合控件 TitleView extends FrameLayout

组合控件的意思就是,我们并不需要自己去绘制视图上显示的内容,而只是用系统原生的控件就好了


顾明思异,就是将各种控件组合起来呗

思路:

  • 不用多想,先初始化各种需要的东西

    • 先贴出要在TitleView里面加入的布局吧

      <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:gravity="center_vertical"    android:layout_height="50dp"    android:background="#ffcb05">    <Button        android:id="@+id/button_left"        android:layout_marginLeft="5dp"        android:layout_width="70dp"        android:background="@null"        android:layout_height="40dp"        android:text="Back"        />    <TextView        android:id="@+id/text_title"        android:gravity="center"        android:text="标题"        android:textColor="#f00"        android:textSize="20sp"        android:layout_centerVertical="true"        android:layout_centerInParent="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout>

初始化

    private void init(){    View view = View.inflate(getContext(), R.layout.text_back,this);    titleText = (TextView) view.findViewById(R.id.text_title);    leftButton = (Button) view.findViewById(R.id.button_left);    leftButton.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            ((Activity)getContext()).finish();            }        });     }

* 实现了点击返回就结束当前Activity,不用再在布局里面添加了,另外,为了让TitleView有更强地扩展性,我们还提供了setTitleText()、setLeftButtonText()、setLeftButtonListener()等方法

    public void setTitleText(String text) {        titleText.setText(text);    }    public void setLeftButtonText(String text){        leftButton.setText(text);    }    public void setLeftButtonListener(OnClickListener l){            leftButton.setOnClickListener(l);    }

效果图

0 0
原创粉丝点击