900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > android自定义组合view 自定义View之组合View

android自定义组合view 自定义View之组合View

时间:2024-06-29 08:30:44

相关推荐

android自定义组合view 自定义View之组合View

前言

自定义View是安卓开发中比较重要的一环,很多地方都需要用到自定义View。而自定义View比较常见的一种形式就是组合View,也是比较简单的一种方式。下面通过一个实例来学习一下自定义组合view。

先来一张效果图吧。

自定义TitleBar

没错,今天就一起来实现一下自定义TitleBar,自定义属性一般是少不了的,先看一下自定义属性文件代码,在value文件夹下,文件名一般为attr.xml 或 attrs.xml,这里我们定义了三个属性:布局的背景色、中间的文本及颜色。

接下来就是MyTitleBar的布局文件:

android:layout_width="match_parent"

android:layout_height="45dp"

android:id="@+id/layout_titlebar_root"

>

android:id="@+id/btn_left"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:src="@mipmap/ico_return"

android:paddingLeft="10dp"

/>

android:id="@+id/tv_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:textSize="16sp"

android:text="" />

android:id="@+id/btn_right"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_centerVertical="true"

android:src="@mipmap/ico_title_right"

android:paddingRight="10dp"

/>

然后就是然后就是最重要的MyTitleBar.java 文件

public class MyTitleBar extends RelativeLayout {

private int mBgColor = Color.BLUE;

private int mTextColor = Color.WHITE;

private String mTitleText = "";

private ImageView btn_left;

private ImageView btn_right;

private TextView tvTitle;

private RelativeLayout relativeLayout;

public MyTitleBar(Context context) {

super(context);

initView(context);

}

public MyTitleBar(Context context, AttributeSet attrs) {

super(context, attrs);

initTypeValue(context,attrs);

initView(context);

}

public MyTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

initTypeValue(context,attrs);

initView(context);

}

public void initTypeValue(Context context ,AttributeSet attrs){

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CusTitleBar);

mBgColor = a.getColor(R.styleable.CusTitleBar_bg_color, Color.YELLOW);

mTitleText = a.getString(R.styleable.CusTitleBar_title_text);

mTextColor = a.getColor(R.styleable.CusTitleBar_text_color,Color.RED);

a.recycle();

}

public void initView(Context context){

LayoutInflater.from(context).inflate(R.layout.layout_custom_titlebar,this,true);

btn_left = findViewById(R.id.btn_left);

btn_right = findViewById(R.id.btn_right);

tvTitle = findViewById(R.id.tv_title);

relativeLayout = findViewById(R.id.layout_titlebar_root);

relativeLayout.setBackgroundColor(mBgColor);

tvTitle.setTextColor(mTextColor);

tvTitle.setText(mTitleText);

}

public void setLeftClickListener(OnClickListener listener){

btn_left.setOnClickListener(listener);

}

public void setRightClickListener(OnClickListener listener){

btn_right.setOnClickListener(listener);

}

public void setTitleText(String str){

if(!TextUtils.isEmpty(str)){

tvTitle.setText(str);

}

}

}

通过以上步骤,我们的自定义View就做好了,下面就看一下怎么调用吧

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="#eee"

tools:context=".MainActivity">

xmlns:apps="/apk/res-auto"

android:id="@+id/my_custom_action_bar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#aaa"

apps:title_text="标题"

apps:text_color = "#fff"

apps:bg_color = "#b3f"

/>

public class MainActivity extends AppCompatActivity {

private MyTitleBar titleBar;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

}

public void init(){

titleBar = findViewById(R.id.my_custom_action_bar);

titleBar.setTitleText("我的标题");

titleBar.setLeftClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(MainActivity.this,"左点击",Toast.LENGTH_SHORT).show();

}

});

titleBar.setRightClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(MainActivity.this,"右点击",Toast.LENGTH_SHORT).show();

}

});

}

}

结语

以上就是自定义MyTitleBar的完整过程。在我们的实际项目中使用,可以简洁我们的代码,提升我们的开发效率。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。