我们写的ActionBar的布局一般相似,只是里面的内容有所改变,当我们遇到多个相似的Actionbar时,可以使用自定义布局,然后在自定义布局中定义修改参数的方法即可
下面是一个简单的带有滑动actionbar以及fragment切换的例子:
1.自定义xml文件,即所要填充的布局,如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android=" http://schemas.android.com/apk/res/android"
android:id="@+id/custom_relative"
android:layout_height="60dp"
android:layout_width="match_parent"
android:gravity="center_vertical"
>
<ImageView
android:id="@+id/custom_iv"
android:layout_height="40dp"
android:layout_width="40dp"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/custom_title"
android:layout_height="40dp"
android:layout_width="100dp"
android:layout_centerInParent="true"
/>
<ProgressBar
android:id="@+id/custom_progress"
android:layout_height="30dp"
android:layout_width="30dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/custom_title"
/>
<TextView
android:id="@+id/custom_tv"
android:layout_height="40dp"
android:layout_width="40dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
/>
</RelativeLayout>
<RelativeLayout
xmlns:android=" http://schemas.android.com/apk/res/android"
android:id="@+id/custom_relative"
android:layout_height="60dp"
android:layout_width="match_parent"
android:gravity="center_vertical"
>
<ImageView
android:id="@+id/custom_iv"
android:layout_height="40dp"
android:layout_width="40dp"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/custom_title"
android:layout_height="40dp"
android:layout_width="100dp"
android:layout_centerInParent="true"
/>
<ProgressBar
android:id="@+id/custom_progress"
android:layout_height="30dp"
android:layout_width="30dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/custom_title"
/>
<TextView
android:id="@+id/custom_tv"
android:layout_height="40dp"
android:layout_width="40dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
/>
</RelativeLayout>
2.自定义类继承所要的布局
在类中定义填充,修改的方法
public class Customview extends RelativeLayout{
private ImageView image;
private TextView text;
private TextView title;
private ProgressBar progress;
private RelativeLayout relativeLayout;
public View view;
public Customview(Context context) {
super(context);
inflate();
}
private void inflate() {
LayoutInflater.from(getContext()).inflate( R.layout.customview,this,true);
findId();
initLayout();
setTitle("111");
setText("aaa");
setImageDrawable(R.drawable.ic_launcher);
setBackground();
}
private void initLayout()
{
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
}
private void findId()
{
relativeLayout=(RelativeLayout) findViewById(R.id.custom_relative);
image=(ImageView) findViewById(R.id.custom_iv);
text=(TextView) findViewById(R.id.custom_tv);
title=(TextView) findViewById(R.id.custom_title);
progress=(ProgressBar) findViewById(R.id.custom_progress);
}
private void setBackground()
{
relativeLayout.setBackgroundColor(Color.WHITE);
}
public void setTitle(String title_name)
{
title.setText(title_name);
title.setGravity(Gravity.CENTER);
}
public void setText(String text_name)
{
text.setText(text_name);
text.setGravity(Gravity.CENTER);
}
private void setImage(Bitmap map)
{
image.setImageBitmap(map);
}
public void setImageDrawable(int res)
{
image.setImageResource(res);
}
}
3.在activity中在需要填充的布局中填充自定义的视图:
可以通过自定义布局中的方法修改参数
public class MainActivity extends FragmentActivity implements OnPageChangeListener{
private ViewPager pager;
private HorizontalScrollView horizontalScrollView;
private LinearLayout linearLayout;
private List<Fragment> list;
private int textWidth;
private ActionBar actionBar;
private TextView indiacate;
private LinearLayout.LayoutParams indiacateLayoutParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findId();
initList();
setListener();
initTextModel();
setTextModel(0);
initActionBar();
}
private void initActionBar()
{
actionBar=getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
Customview customView=new Customview(this);
customView.setTitle("title");
customView.setImageDrawable(R.drawable.collect);
actionBar.setCustomView(customView);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.show();
}
private void initList()
{
list=new ArrayList<Fragment>();
for(int i=0;i<linearLayout.getChildCount();i++)
{
list.add(new MyFragment());
}
}
private void findId()
{
pager=(ViewPager) findViewById(R.id.main_viewpager);
horizontalScrollView=(HorizontalScrollView) findViewById(R.id.main_horizontalScrollView);
linearLayout=(LinearLayout) findViewById(R.id.main_linearLayout);
indiacate=(TextView) findViewById(R.id.main_indiacate);
indiacateLayoutParams=(android.widget.LinearLayout.LayoutParams) indiacate.getLayoutParams();
}
private void initTextModel()
{
for(int i=0;i<linearLayout.getChildCount();i++)
{
TextView tv=(TextView) linearLayout.getChildAt(i);
LayoutParams indicateParams = (LinearLayout.LayoutParams) tv
.getLayoutParams();
textWidth=indicateParams.width;
Log.i("123","------>"+textWidth);
tv.setTag(i);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position =Integer.parseInt(v.getTag().toString());
pager.setCurrentItem(position);
horizontalScrollView.scrollTo(textWidth*position, 0);
}
});
}
}
private void setTextModel(int position)
{
for(int i=0;i<linearLayout.getChildCount();i++)
{
TextView tv=(TextView) linearLayout.getChildAt(i);
tv.setTextColor(Color.BLACK);
}
TextView tv2=(TextView) linearLayout.getChildAt(position);
tv2.setTextColor(Color.RED);
indiacateLayoutParams.setMargins(textWidth*position, 0, 0, 0);
}
private void setListener()
{
MyAdapter adapter=new MyAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
pager.setOnPageChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int position, float arg1, int offPix) {
// TODO Auto-generated method stub
horizontalScrollView.scrollTo(textWidth*position+offPix/2, 0);
indiacateLayoutParams.setMargins(textWidth*position+offPix/2, 0, 0, 0);
indiacate.setLayoutParams(indiacateLayoutParams);
}
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
setTextModel(position);
// horizontalScrollView.scrollTo(textWidth*position, 0);
}
}