最近閑來無事,各個新上映的電影都看了個遍,想着自己不能這麼放縱,想寫一寫初學的時候學習的Android自定義頭部控件,完了,忘記怎麼寫了。

31563785323_.pic.jpg
大概要整一個什麼樣的:
1.左邊有圖示的
2.中間有Title的
3.右邊有ImageView
4.右邊有個TextView
3和4重疊
類似這樣子,現在頭部太花哨了,找示例圖都找不到,像16年那會都是這種類型的,不管了,如果需要學這個同學一定是基礎,那麼就當一個入門吧。

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,在這裡踩過坑的人找了好久才找到)

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);
}
}

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">

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();
}
});
}
}

41563785324_.pic.jpg