自定义view继承RelativeLayout
来源:互联网 发布:在墨尔本生活知乎 编辑:程序博客网 时间:2024/05/20 06:23
一.实现思路
先创建一个继承RelativeLayout的类,创建与它相对应的布局,布局随意。给它一个方法获取控件id,然后设置基本属性内容,最后设置监听。
创建attrs设置标题栏的基本属性,然后在activity_main中设置这个自定义控件。
二.实现的代码步骤
titleset.xml布局,设置一个自定义标题栏,控件自己可以随意设计
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:background="@color/colorAccent" android:layout_width="match_parent" android:layout_height="56dp" > <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <ImageView android:id="@+id/imageview" android:layout_width="100dp" android:layout_height="100dp" /> </RelativeLayout>
在values里创建attrs.xml,通过这个布局调用在主布局中设置的属性
<declare-styleable name="TitleSet"><!--设置标题的颜色--> <attr name="MYZT" format="color" /> <!--设置标题内容--> <attr name="MYR" format="string" /> <!--设置标题大小--> <attr name="mysize" format="integer" /> <!--设置图片颜色--> <attr name="photo" format="color"/> </declare-styleable>
创建TitleSet类继承RelativeLayout
public class TitleSet extends RelativeLayout { TextView textView;//标题 ImageView imageView;//图片public BarClickListener listener;//监听 public void setListener(BarClickListener listener) { this.listener = listener; } /** * 一种参数 * @param context */ public TitleSet(Context context) { super(context); initview(null); } /** * 两种参数 * @param context * @param attrs */ public TitleSet(Context context, AttributeSet attrs) { super(context, attrs); initview(attrs); } private void initview(AttributeSet attrs) { //获取布局 View v = LayoutInflater.from(getContext()).inflate(R.layout.titleset, this); textView = (TextView) v.findViewById(R.id.textview); imageView = (ImageView) v.findViewById(R.id.imageview); //标题的监听textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(listener!=null){ listener.titleListener(v); } }});//图片的监听 imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (listener!=null){ listener.imageListener(v); } } }); //attrs判空 if (attrs != null) { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TitleSet); //标题栏 String str = a.getString(R.styleable.TitleSet_MYR); //设置字体颜色,默认黑色 int co= a.getColor(R.styleable.TitleSet_MYZT, Color.BLACK); textView.setTextColor(co); //设置字体大小 int size= a.getInteger(R.styleable.TitleSet_mysize,20); textView.setTextSize(size); //设置图片的颜色,默认红色 int ph= a.getColor(R.styleable.TitleSet_photo,Color.RED); imageView.setBackgroundColor(ph); ///如果这是一个空的 if (TextUtils.isEmpty(str)) { //就默认项目名称 textView.setText(getResources().getString(R.string.app_name)); } else { textView.setText(str); } a.recycle();//一定记得销毁 } } //设置监听的方法public interface BarClickListener{ public void imageListener(View v); public void titleListener(View v);}}
activity_main.xml布局
<com.example.rikaojineng8_04.TitleSet android:id="@+id/title" android:layout_width="match_parent" android:layout_height="match_parent" app:MYR="标题栏" app:mysize="25" app:photo="#f3f3" app:MYZT="#000" />
MainActivity类
public class MainActivity extends AppCompatActivity implements TitleSet.BarClickListener{ TitleSet titleSet; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);titleSet= (TitleSet) findViewById(R.id.title); titleSet.setListener(this); } @Override public void imageListener(View v) { Toast.makeText(this,"图片",Toast.LENGTH_LONG).show(); } @Override public void titleListener(View v) { Toast.makeText(this,"标题",Toast.LENGTH_LONG).show(); }}
阅读全文
0 0
- 自定义view继承RelativeLayout
- 自定义RelativeLayout view实现布局。
- 自定义View继承view
- 自定义View---继承View
- 自定义View,继承View
- 自定义view继承view
- 自定义View-继承系统View
- 自定义view之继承view
- 自定义View之 继承View
- 继承View自定义控件
- 自定义view 之 继承
- 自定义控件-继承View
- 自定义view继承surfaceview
- android自定义View-继承
- 继承View 自定义开关
- 自定义View继承ViewGroup
- 自定义view继承Button
- 自定义View继承ViewGroup
- 内存池、线程池、进程池
- 牛课网 Java专项练习
- jQuery---JQuery remove(),removeAttr()和removeClass()示例区别
- Hotspot JVM垃圾回收器
- REST API 简介(CXFServlet)
- 自定义view继承RelativeLayout
- 反素数
- shiro进阶—与javaweb整合
- 性能与可靠性要求说明书-模板
- 大数欧拉
- poj 3680(最小费用流)
- IDL中数据导出为栅格文件的方式
- 九九乘法表
- D