自定义一个横排类目框

package com.example.publicapp.pem;


import java.util.ArrayList;
import java.util.List;


import com.example.publicapp.R;




import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


public class PemTabHostHead extends LinearLayout{
private Context mContext;
private LayoutInflater inflater;
private View content;
private LinearLayout tabLayout;
private List<PemTab> tabs;
private OnTabChangedListener mOnTabChangedListener;
public PemTabHostHead(Context context) {
super(context);
init(context, null, 0);
}


public PemTabHostHead(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}


public PemTabHostHead(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}


private void init(Context context, AttributeSet attrs, int defStyle) {
mContext = context;
inflater = LayoutInflater.from(context);
content = inflater.inflate(R.layout.pem_tab_host_head, this);
tabs = new ArrayList<PemTab>();
tabLayout = (LinearLayout) content.findViewById(R.id.pem_tab_host_head_layout);
}
public void removeAllTabs(){
tabLayout.removeAllViews();
tabs.clear();
}
public void addTab(String title, float weight){
PemTab tab = new PemTab();
tab.setTitle(title);
//TextView newTabView = (TextView) inflater.inflate(R.layout.pem_tab_text_view, null);
TextView newTabView = new TextView(mContext);
initTabView(newTabView, title, weight);
tab.setView(newTabView);
tab.setIndex(tabs.size());
tabs.add(tab);
final int curTabCount = tabs.size();
newTabView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
selectTab(curTabCount - 1);
if(mOnTabChangedListener != null){
mOnTabChangedListener.onTabChanged(curTabCount - 1);
}
}
});
tabLayout.addView(newTabView);
}


private void initTabView(TextView tabView, String text, float weight) {
LinearLayout.LayoutParams params  = new LinearLayout.LayoutParams(
0, 
LinearLayout.LayoutParams.WRAP_CONTENT);;
params.weight = weight;
tabView.setPadding(5, 5, 5, 5);
tabView.setLayoutParams(params);
tabView.setTextSize(15);
tabView.setSingleLine();
tabView.setGravity(Gravity.CENTER);
tabView.setTextColor(mContext.getResources().getColor(R.color.black));

tabView.setText(text);
tabView.setBackgroundColor(mContext.getResources().getColor(R.color.black));
//tabView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pem_tab_normal));

}


public void selectTab(int tabIndex) {
if(tabIndex < -1 || tabIndex >= tabs.size()){
throw new IndexOutOfBoundsException();
}
for(int i = 0; i < tabs.size(); i++){
if(i == tabIndex){
PemTab selectedTab = tabs.get(i);
TextView tabView = selectedTab.getView();
tabView.setTextColor(android.graphics.Color.parseColor("#1968A6"));
tabView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pem_tab_selected));
//tabView.setBackgroundColor(mContext.getResources().getColor(R.color.pem_gray));
}
else{
PemTab selectedTab = tabs.get(i);
TextView tabView = selectedTab.getView();
tabView.setTextColor(Color.BLACK);
tabView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pem_tab_normal));
//tabView.setBackgroundColor(mContext.getResources().getColor(R.color.whitesmoke));
}
}
//Toast.makeText(mContext, "cur tab " + tabIndex + ", " + selectedTab.getTitle(), Toast.LENGTH_SHORT).show();
}
public void setOnTabChangedListener(OnTabChangedListener onTabChangedListener){
mOnTabChangedListener = onTabChangedListener;
}
public interface OnTabChangedListener{
void onTabChanged(int position);
}

class PemTab{
private String title;
private TextView mView;
private int index;

public PemTab() {
super();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public TextView getView() {
return mView;
}
public void setView(TextView mView) {
this.mView = mView;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}

}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值