public class CompositeViews extends RelativeLayout{ public CompositeViews(Context context) {this(context,null); } public CompositeViews(Context context, AttributeSet attrs) {this(context, attrs,0); } public CompositeViews(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr); } }接下来,我们再定义三个TextView控件,分别是mLefeText,mRightText,textTitle用来显示“返回”,“搜索”以及“标题”并且使用LayoutParams规定它们在容器里面的对齐方式。请看代码:
public class CompositeViews extends RelativeLayout{ private TextView mLefeText; private TextView mRightText; private TextView textTitle; private LayoutParams leftLayoutParams; private LayoutParams ridhtLayoutParams; private LayoutParams titleLayoutParams; public CompositeViews(Context context) {this(context,null); } public CompositeViews(Context context, AttributeSet attrs) {this(context, attrs,0); } public CompositeViews(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initView(context); }initView(context)方法中是用来初始化三个组合控件的,请看:
private void initView(Context context) {mLefeText = new TextView(context);mRightText = new TextView(context);textTitle = new TextView(context);/* * 左按钮位置 */leftLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);leftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);mLefeText.setText("返回");mLefeText.setTextSize(22);/* * 右按钮位置 */ridhtLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);ridhtLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);mRightText.setText("搜索");mRightText.setTextSize(22);/* * 中间标题位置 */titleLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);titleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);textTitle.setText("这是一个标题");textTitle.setTextSize(22); }ok,以上的代码已经实现了组合控件的显示和对齐方式,我们把定义的View添加到布局文件中并在Activity加载吧
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:background="#999999" android:layout_height="wrap_content" android:orientation="vertical"> <com.sanhuimusic.mycustomview.view.CompositeViewsandroid:id="@+id/topBar"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>MainActivity:
public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); }}我们先来运行看下结果
ok,已显示出来了,但是相信大家也看出来了,这上面的代码中,各个控件中的属性是都是我们固定写死的,既然我们是创建可服用的控件,固定写死的东西肯定是不可取的,那么我们怎么可以灵活地获取控件的属性,以至于能达到复用呢?
这就必须要接触另外一种技术了,就是自定义属性。用我们自定义的属于可以在每次使用我们定义的控件时为其分配属性即可。下面我们来学习下自定义属性。
自定义属性其实也是相当的简单,首先,我们现在资源文件res下values目录下新建一个attrs.xml文件(eclipse自带,as自建),新建的attrs.xml是一个包含如下代码的文件:
<?xml version="1.0" encoding="utf-8"?><resources></resources>在resources中有各种属性供我们使用,同学们可以自己看下。根据我们现在的需求,我们选择使用declare-styleable来声明我们的属性集,然后为其定义特有的name属性,这个name是供我们在使用自定义属性时,通过它可以查找到里面的所有属性。请看如下代码:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="CompositeViews"><attr name="titleText" format="string"/><attr name="titleTextSize" format="dimension"/><attr name="titleColor" format="color"/><attr name="titleBackground" format="color|reference"/><attr name="leftTextColor" format="color"/><attr name="leftBackground" format="color|reference"/><attr name="leftText" format="string"/><attr name="leftTextSize" format="dimension"/><attr name="rightTextColor" format="color"/><attr name="rightBackground" format="color|reference"/><attr name="rightText" format="string"/><attr name="rightTextSize" format="dimension"/> </declare-styleable></resources>单独拿一行属性来解析下它所代表的含义:如
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:background="#999999" android:layout_height="wrap_content" android:orientation="vertical"> <com.sanhuimusic.mycustomview.view.CompositeViewsandroid:id="@+id/topBar"android:layout_width="wrap_content"android:layout_height="wrap_content"custom:titleText="@string/titleText"custom:titleColor="#000000"custom:titleTextSize="@dimen/titleTextSize"custom:titleBackground="#999999"custom:leftText="@string/leftText"custom:leftTextColor="#FFFFFF"custom:leftBackground="#666666"custom:leftTextSize="@dimen/leftTextSize"custom:rightText="@string/rightText"custom:rightTextColor="#FFFFFF"custom:rightBackground="#666666"custom:rightTextSize="@dimen/rightTextSize"/> </LinearLayout>我们是使用custom加上我们自定义属性里面< attr name="titleText" format="string"/>里的name值来动态设置属性值的,如:custom:titleText="@string/titleText"。
private void initView(Context context, AttributeSet attrs) {mLefeText = new TextView(context);mRightText = new TextView(context);textTitle = new TextView(context);/** * 获取自定义属性 */TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CompositeViews);String titleText = typedArray.getString(R.styleable.CompositeViews_titleText);float titleTextSize = typedArray.getDimension(R.styleable.CompositeViews_titleTextSize, 16);int titleColor = typedArray.getColor(R.styleable.CompositeViews_titleColor,0);Drawable titleBackground = typedArray.getDrawable(R.styleable.CompositeViews_titleBackground);String leftText = typedArray.getString(R.styleable.CompositeViews_leftText);int leftTextColor = typedArray.getColor(R.styleable.CompositeViews_leftTextColor, 0);float leftTextSize = typedArray.getDimension(R.styleable.CompositeViews_leftTextSize, 16);Drawable leftBackground = typedArray.getDrawable(R.styleable.CompositeViews_leftBackground);String rightText = typedArray.getString(R.styleable.CompositeViews_rightText);int rightTextColor = typedArray.getColor(R.styleable.CompositeViews_rightTextColor, 0);float rightTextSize = typedArray.getDimension(R.styleable.CompositeViews_rightTextSize, 16);Drawable rightBackground = typedArray.getDrawable(R.styleable.CompositeViews_rightBackground);typedArray.recycle();/* * 左按钮位置 */leftLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);leftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);mLefeText.setText(leftText);mLefeText.setTextColor(leftTextColor);mLefeText.setTextSize(leftTextSize);mLefeText.setBackground(leftBackground);addView(this.mLefeText,leftLayoutParams); /* * 右按钮位置 */ridhtLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);ridhtLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);mRightText.setText(rightText);mRightText.setTextColor(rightTextColor);mRightText.setTextSize(rightTextSize);mRightText.setBackground(rightBackground);addView(mRightText,ridhtLayoutParams);/* * 中间标题位置 */titleLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);titleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);textTitle.setText(titleText);textTitle.setTextSize(titleTextSize);textTitle.setTextColor(titleColor);textTitle.setBackground(titleBackground);addView(textTitle,titleLayoutParams); }代码解释:首先通过上下文context获取到属性存放到TypedArray 中,然后通过TypedArray 里封装好的各种方法获取对应的属性值,然后再分别为我们的控件设置属性。这样就完成了,自定义属性的使用,并且复用度高,每当需要使用标题栏是都只需要在xml中添加我们定义的View控件,为其配置属性即可使用,节约了开发时间,提高了效率,并且还保持的app风格的一致。
public interface TopBarClickListener{ void leftClickListener(); void rightClickListener(); }然后在构造方法中为左右控件添加点击事件,但不实现功能,等待调用者自己实现:
private void setListener() {mLefeText.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {mTopBarClickListener.leftClickListener(); }});mRightText.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {mTopBarClickListener.rightClickListener(); }}); }再者,把定义好的接口暴露给调用者:
public void setOnTopBarClickListener(TopBarClickListener topBarClickListener){mTopBarClickListener = topBarClickListener; }最后,谁调用,谁实现。这就完成了不同界面复用控件实现不同的功能的便利。在这里我们只在MainActivity中打印Toast就可以了。
public class MainActivity extends AppCompatActivity { private CompositeViews topBar; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);topBar = (CompositeViews) findViewById(R.id.topBar);topBar.setOnTopBarClickListener(new CompositeViews.TopBarClickListener(){ @Override public void leftClickListener() {ToastUtil.makeText(MainActivity.this,"您点击了返回键",Toast.LENGTH_SHORT).show(); } @Override public void rightClickListener() {ToastUtil.makeText(MainActivity.this,"您点击了搜索键",Toast.LENGTH_SHORT).show(); }}); }}OK,看看结果吧
好,已经可以实现我们的需求了,是不是学会很多呢。
今天主要讲了android自定义View中另一种的实现,并且还学习了自定义属性,同学们下去好好消化下,并自己动手现实一两个例子吧,好了,今天就讲到这里,谢谢大家。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。