天天看点

android 自定义头部,闲来无事实现-Android自定义头部控件

最近闲来无事,各个新上映的电影都看了个遍,想着自己不能这么放纵,想写一写初学的时候学习的Android自定义头部控件,完了,忘记怎么写了。

android 自定义头部,闲来无事实现-Android自定义头部控件

31563785323_.pic.jpg

大概要整一个什么样的:

1.左边有图标的

2.中间有Title的

3.右边有ImageView

4.右边有个TextView

3和4重叠

类似这样子,现在头部太花哨了,找示例图都找不到,像16年那会都是这种类型的,不管了,如果需要学这个同学一定是基础,那么就当一个入门吧。

android 自定义头部,闲来无事实现-Android自定义头部控件

image.png

做一个类似的吧。

1.首先把布局摆上来

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginTop="5dp"

android:background="@color/white">

android:id="@+id/iv_left"

android:layout_width="20dp"

android:layout_height="20dp"

android:layout_marginLeft="5dp"/>

android:id="@+id/tv_center"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="title"

android:textSize="18sp"

android:layout_centerHorizontal="true"/>

android:id="@+id/iv_right"

android:layout_width="20dp"

android:layout_height="20dp"

android:layout_marginRight="5dp"

android:layout_alignParentRight="true"/>

android:id="@+id/tv_right"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="edit"

android:layout_marginRight="5dp"

android:layout_alignParentRight="true"/>

2.attr.xml文件(注意了啊!注意!图片格式是reference,在这里踩过坑的人找了好久才找到)

android 自定义头部,闲来无事实现-Android自定义头部控件

51563785325_.pic.jpg

3.把TitleBar放上来(到这里基本就完事了)

public class TitleBar extends RelativeLayout{

private ImageView ivleft;

private TextView tvCenter;

private TextView tvRight;

private ImageView ivRight;

public TitleBar(Context context, AttributeSet attrs) {

super(context, attrs);

initView(context,attrs);

}

private void initView(Context context,AttributeSet attributeSet){

View inflater = LayoutInflater.from(context).inflate(R.layout.title_bar,this);

ivleft = inflater.findViewById(R.id.iv_left);

tvCenter = inflater.findViewById(R.id.tv_center);

tvRight = inflater.findViewById(R.id.tv_right);

ivRight = inflater.findViewById(R.id.iv_right);

init(context,attributeSet);

}

public void init(Context context,AttributeSet attributeSet){

TypedArray typedArray = context.obtainStyledAttributes(attributeSet,R.styleable.TitleBar);

int leftIcon = typedArray.getResourceId(R.styleable.TitleBar_LeftIcon,R.drawable.back);

String titleContent = typedArray.getString(R.styleable.TitleBar_TitleContent);

int rightIcon = typedArray.getResourceId(R.styleable.TitleBar_RightIcon,R.drawable.edit);

String rightContent = typedArray.getString(R.styleable.TitleBar_RightContent);

int titleType = typedArray.getInt(R.styleable.TitleBar_TitleType,0);

ivleft.setImageResource(leftIcon);

tvCenter.setText(titleContent);

ivRight.setImageResource(rightIcon);

tvRight.setText(rightContent);

if(titleType==0){

ivRight.setVisibility(View.GONE);

tvRight.setVisibility(View.VISIBLE);

}else{

ivRight.setVisibility(View.VISIBLE);

tvRight.setVisibility(View.GONE);

}

}

public void setLeftButtonOnClickListener(OnClickListener l){

ivleft.setOnClickListener(l);

}

public void setIvRightOnClickListener(OnClickListener l){

ivRight.setOnClickListener(l);

}

public void setTvRightOnclickListener(OnClickListener l){

tvRight.setOnClickListener(l);

}

}

android 自定义头部,闲来无事实现-Android自定义头部控件

image.png

4.在布局文件里放上引用的自定义头部类()

android:id="@+id/title_bar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#DCDCDC"

app:LeftIcon="@drawable/back"

app:RightContent="发说说"

app:RightIcon="@drawable/edit"

app:TitleContent="好友动态"

app:TitleType="0">

android 自定义头部,闲来无事实现-Android自定义头部控件

21563785322_.pic_hd.jpg

5.最后!~MainActivity里

public class MainActivity extends AppCompatActivity {

private TitleBar titleBar;

private ImageView tvLeft;

private TextView tvRight;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

titleBar = findViewById(R.id.title_bar);

titleBar.setLeftButtonOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Toast.makeText(MainActivity.this,"Sdf",Toast.LENGTH_SHORT).show();

}

});

}

}

android 自定义头部,闲来无事实现-Android自定义头部控件

41563785324_.pic.jpg