模板的写法,写一个titleBar

来源:互联网 发布:淘宝店宝贝描述 编辑:程序博客网 时间:2024/04/19 01:23

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">如果我们接触到一个相对比较大型的项目,将可能会在布局方面会有很多类似的布局,此时如果每一个布局我们都要重新写,则会浪费时间,降低效率,所以此时使用模板方法制作一个模板显得尤为必要,这个模板的使用方法就跟android中的控件一样,只需设置相应的参数即可。这样是不是显得方便许多。</span>


第一步:模板方法首先要做的便是定义一些属性,我们需要在values文件夹下创建一个atts.xml文件,在里面写下我们需要设置的属性,因为titlebar通常都是中间是文字,两边各有两个按钮,代码如下:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="Topbar">        <attr name="title" format="string"/>        <attr name="titleTextSize" format="dimension"/>        <attr name="titleTextColor" format="color"/>        <attr name="leftTextColor" format="color"/>        <attr name="leftBackground" format="reference|color"/>        <attr name="leftText" format="string"/>        <attr name="rightTextColor" format="color"/>        <attr name="rightBackground" format="reference|color"/>        <attr name="rightText" format="string"/>    </declare-styleable></resources>


第二步:我们需要在布局文件中放进这个控件

代码如下:

<com.jinglion.moviediscusser.Topbar        android:id="@+id/topbar"        android:layout_width="match_parent"        android:layout_height="45dp"        custom:leftBackground="@drawable/btn_all_back"        custom:leftText=""        custom:leftTextColor="#FFFFFF"        custom:rightBackground="@drawable/blue_button"        custom:rightText="MORE"        custom:rightTextColor="#FFFFFF"        custom:title="机器人客服:小影"        custom:titleTextColor="#123412"        custom:titleTextSize="10sp">
此处我们可以看到,这里我们只需要直接使用atts.xml里面设置好的属性,并为其设置相应的属性值即可。

但此时你可能会发现程序会报错,那就是因为你没有设置命名空间,我们通常使用属性时都是android:xxxxx=""这样的形式,那是因为我们的xml文件里面默认会有xmlns:android="http://schemas.android.com/apk/res/android"这样一行代码,这就是命名空间。我们也可以自己设置相应的命名空间,然后才能在属性中使用命名空间中的属性,这里我们就要说一下命名空间是如何命名的:xmlns:<命名空间名>="http://schemas.android.com/apk/res/<对应类所在的包名>",加上这样一句代码后便不会再报错。


第三步我们需要创建一个类在加载和解析资源数据

注意该类类名需与第一步中的属性的name属性一致,否则会出现错误,

代码如下:

import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.TextView;public class Topbar extends RelativeLayout {private Button leftButton, rightButton;private TextView tvTitle;private int leftTextColor;private Drawable leftBackground;private String leftText;private int rightTextColor;private Drawable rightBackground;private String rightText;private float titleTextSize;private int titleTextColor;private String title;private LayoutParams leftParams, rightParams, titleParams;public Topbar(Context context, AttributeSet attrs) {super(context, attrs);TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar);leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);leftText = ta.getString(R.styleable.Topbar_leftText);rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);rightText = ta.getString(R.styleable.Topbar_rightText);titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);title = ta.getString(R.styleable.Topbar_title);//回收,避免浪费资源和由于缓存产生的错误ta.recycle();leftButton = new Button(context);rightButton = new Button(context);tvTitle = new TextView(context);leftButton.setTextColor(leftTextColor);leftButton.setBackground(leftBackground);leftButton.setText(leftText);rightButton.setTextColor(rightTextColor);rightButton.setBackground(rightBackground);rightButton.setText(rightText);tvTitle.setTextColor(titleTextColor);tvTitle.setTextSize(titleTextSize);tvTitle.setText(title);tvTitle.setGravity(Gravity.CENTER);setBackgroundColor(0xFFF59563);leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);addView(leftButton, leftParams);//把leftButton 以leftParams的形式添加进去rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);addView(rightButton, rightParams);//把rightButton 以rightParams的形式添加进去titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);addView(tvTitle, titleParams);}}
由上面的代码可以看出,我们通过
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar);
代码加载atts.xml文件夹下的name值为Topbar的属性值,并通过ta.getXXX()方法获取相应的属性值,属性值是在布局文件中设置的。


leftButton = new Button(context);
leftButton.setTextColor(leftTextColor);

创建一个Button对象,并为其设置相应的参数。


private LayoutParams leftParams, rightParams, titleParams;

leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);

此处定义了布局参数的定义器,并通过new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);定义相应子控件的属性。


addView(leftButton, leftParams);//把leftButton 以leftParams的形式添加进去。

此处正式将子控件以自己定义好的属性值添加进去。把leftButton 以leftParams的形式添加进去。

此时我们的自定义控件便完成了。

但我们还想再为按钮添加点击事件,则需要添加点击事件,并提供设置按钮是否可见的方法。

        private topbarClickListener listener;public interface topbarClickListener{public void leftClick();public void rightClick();}public void setOnTopbarClickListener(topbarClickListener listener){this.listener = listener;}        public void setLeftIsVisable(boolean flag){if (flag) {leftButton.setVisibility(View.VISIBLE);} else {leftButton.setVisibility(View.GONE);}}public void setRightIsVisable(boolean flag){if (flag) {rightButton.setVisibility(View.VISIBLE);} else {rightButton.setVisibility(View.GONE);}}
此处采用了接口的回调机制的思想。

代码整合完整如下:

import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.TextView;public class Topbar extends RelativeLayout {private Button leftButton, rightButton;private TextView tvTitle;private int leftTextColor;private Drawable leftBackground;private String leftText;private int rightTextColor;private Drawable rightBackground;private String rightText;private float titleTextSize;private int titleTextColor;private String title;private LayoutParams leftParams, rightParams, titleParams;private topbarClickListener listener;public interface topbarClickListener{public void leftClick();public void rightClick();}public void setOnTopbarClickListener(topbarClickListener listener){this.listener = listener;}public Topbar(Context context, AttributeSet attrs) {super(context, attrs);TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar);leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);leftText = ta.getString(R.styleable.Topbar_leftText);rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);rightText = ta.getString(R.styleable.Topbar_rightText);titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);title = ta.getString(R.styleable.Topbar_title);//回收,避免浪费资源和由于缓存产生的错误ta.recycle();leftButton = new Button(context);rightButton = new Button(context);tvTitle = new TextView(context);leftButton.setTextColor(leftTextColor);leftButton.setBackground(leftBackground);leftButton.setText(leftText);rightButton.setTextColor(rightTextColor);rightButton.setBackground(rightBackground);rightButton.setText(rightText);tvTitle.setTextColor(titleTextColor);tvTitle.setTextSize(titleTextSize);tvTitle.setText(title);tvTitle.setGravity(Gravity.CENTER);setBackgroundColor(0xFFF59563);leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);addView(leftButton, leftParams);//把leftButton 以leftParams的形式添加进去rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);addView(rightButton, rightParams);//把rightButton 以rightParams的形式添加进去titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);addView(tvTitle, titleParams);leftButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {listener.leftClick();}});rightButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {listener.rightClick();}});}public void setLeftIsVisable(boolean flag){if (flag) {leftButton.setVisibility(View.VISIBLE);} else {leftButton.setVisibility(View.GONE);}}public void setRightIsVisable(boolean flag){if (flag) {rightButton.setVisibility(View.VISIBLE);} else {rightButton.setVisibility(View.GONE);}}}

至此,模板便彻底完成了。

那么接下来我们就要问了,模板创建出来也了,也为按钮添加了点击事件,我们又改如何使用呢?

其实模板的优点就在于此,它的使用方法和其他空间一模一样。

首先,找到控件

topbar = (Topbar) findViewById(R.id.topbar);

topbar.setLeftIsVisable(true);
topbar.setRightIsVisable(false);//此处我根据自己的项目,右边按钮不显示。

然后设置点击事件并重写相应的方法。

topbar.setOnTopbarClickListener(new topbarClickListener() {

@Override
public void rightClick() {

}

@Override
public void leftClick() {
finish();
}
});


0 0
原创粉丝点击