先创建一个标题栏的类,需要用到 foot_bar_item.xml 和 FootBarActionParmater类。
package pageview.footBar;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.lin_ling.mynfcapplication01.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ty-deng on 17/5/6.
*/
public class FootBar extends LinearLayout {
Context context;
List<FootBarActionParmater> dataScoure = new ArrayList<FootBarActionParmater>();
ActionParmaterListener listener;
int selectPosition = 0;
public interface ActionParmaterListener{
void ActionListener(int position);
}
public FootBar(Context context) {
super(context);
this.context = context;
}
public FootBar(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public FootBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
}
public void setFootBarActionParmater(List<FootBarActionParmater> dataScoure,ActionParmaterListener mlistener){
this.dataScoure = dataScoure;
this.listener = mlistener;
setOrientation(LinearLayout.HORIZONTAL);
DisplayMetrics dm = getResources().getDisplayMetrics();
int width = dm.widthPixels/dataScoure.size();
for (int i = 0;i<dataScoure.size();i++){
final FootBarActionParmater parmater = dataScoure.get(i);
LayoutInflater layoutInflater = LayoutInflater.from(context);
LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.foot_bar_item,null);
linearLayout.setLayoutParams(new LayoutParams(width,LayoutParams.WRAP_CONTENT));
final ImageView imageView = (ImageView)linearLayout.findViewById(R.id.imageView1);
imageView.setImageResource(parmater.imageNameResourceId);
final TextView barView = (TextView) linearLayout.findViewById(R.id.textView1);
barView.setTextColor(Color.WHITE);
barView.setText(parmater.title);
final int position = i;
parmater.imageView = imageView;
parmater.barview = barView;
linearLayout.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
if(listener != null){
selectBar(position);
listener.ActionListener(position);
}
}
});
addView(linearLayout);
}
}
public void selectBar(int positon){
FootBarActionParmater parmater = dataScoure.get(selectPosition);
parmater.imageView.setImageResource(parmater.imageNameResourceId);
FootBarActionParmater parmaterSelect = dataScoure.get(positon);
parmaterSelect.imageView.setImageResource(parmaterSelect.imageNameResourceIdSelected);
selectPosition = positon;
}
public int getResource(String imageName){
int resId = getResources().getIdentifier(imageName, "drawable", context.getPackageName());
//如果没有在"mipmap"下找到imageName,将会返回0
return resId;
}
}
FootBarActionParmater类
package pageview.footBar;
/**
* Created by ty-deng on 17/5/6.
*/
public class FootBarActionParmater{
public String title;
public int imageNameResourceId;
public int imageNameResourceIdSelected;
public ImageView imageView;
public TextView barview;
public FootBarActionParmater(String title,int imageNameResourceId,int imageNameResourceIdSelected){
this.title =title;
this.imageNameResourceId = imageNameResourceId;
this.imageNameResourceIdSelected = imageNameResourceIdSelected;
}
}
foot_bar_item.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="25sp"
android:layout_height="25sp"
android:layout_gravity="center_horizontal"
android:paddingTop="5sp"
android:layout_marginTop="5sp"
android:id="@+id/imageView1" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5sp"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:id="@+id/textView1" />
</LinearLayout>
使用布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:background="@drawable/blue_background_change"
android:layout_height="match_parent">
<include layout="@layout/my_title_bar"
android:id="@+id/include" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/contectView"
android:orientation="vertical">
<pageview.footBar.FootBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff4f4f4"
android:id="@+id/footbar">
</pageview.footBar.FootBar>
</LinearLayout>
</RelativeLayout>
代码使用使用
FootBar footBar = (FootBar) findViewById(R.id.footbar);
List<FootBarActionParmater> dataScoure = new ArrayList<FootBarActionParmater>();
FootBarActionParmater fooBar = new FootBarActionParmater("防伪查询",R.drawable.blue_background_change);
dataScoure.add(fooBar);
FootBarActionParmater fooBar1 = new FootBarActionParmater("授权查询",R.drawable.blue_background_change);
dataScoure.add(fooBar1);
FootBarActionParmater fooBar2 = new FootBarActionParmater("后台",R.drawable.blue_background_change);
dataScoure.add(fooBar2);
footBar.setFootBarActionParmater(dataScoure, new FootBar.ActionParmaterListener() {
@Override
public void ActionListener(int position, ImageView imageView, TextView barView) {
}
});