如何使自定义操作栏在使用swipable选项卡视图时填充屏幕宽度?
问题描述:
嗨我想要具有swipable选项卡视图的自定义操作栏。我已经创建了自定义操作栏,并且即时设置了操作栏的自定义视图。当我和其他屏幕一起使用时,动作栏效果很好,但是当我将它与TabView一起使用时,应用程序图标出现在动作栏的左侧。请帮助我使自定义操作栏在tabview上填充整个宽度。我的代码是这样的,如何使自定义操作栏在使用swipable选项卡视图时填充屏幕宽度?
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tab_bar);
final Animation animLogOut=AnimationUtils.loadAnimation(this, R.anim.animzoom);
final Animation animBack=AnimationUtils.loadAnimation(this, R.anim.animzoom);
// final Animation animHome=AnimationUtils.loadAnimation(this, R.anim.animzoom);
//2 ActionBar objects for custom action bar and Tab view
mActionBar=getActionBar();
actionBar = getActionBar();
viewPager = (ViewPager)findViewById(R.id.pager);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.actionbar, null);
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
mActionBar.setDisplayShowCustomEnabled(true);
mActionBar.setCustomView(mCustomView, lp1);
imLogout = (ImageView)mCustomView.findViewById(R.id.imlogout);
imBack = (ImageView)mCustomView.findViewById(R.id.imback);
imHome = (ImageView)mCustomView.findViewById(R.id.home);
imHome.setVisibility(View.INVISIBLE);
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Adding tabs
for(String tab_name: tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
/**
* on swiping the viewPager make respective tab selected
*/
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position){
//on changing the page
//make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
EDIT1:这里是我的自定义操作栏布局,conatils 4个imageViews。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llheader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:background="@color/headerbackground"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imlogout"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="6dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="@drawable/logout" />
<ImageView
android:id="@+id/home"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/imlogout"
android:padding="6dp"
android:src="@drawable/home" />
<ImageView
android:id="@+id/productlogo"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/imback"
android:paddingBottom="7dp"
android:src="@drawable/harmaan" />
<ImageView
android:id="@+id/imback"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="6dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/back_icon" />
</RelativeLayout>
并且屏幕上的应用程序徽标出现在左侧。由于低信誉我不能发布屏幕schot。请帮我解决这个问题。
答
改变此密码
LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
通过
ActionBar.LayoutParams lp1 = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
然后检查希望这将有助于你
+0
不,它没有工作... – Thejas 2014-11-22 10:07:17
你可以把动作条XML代码? – 2014-11-22 09:34:52
你已经把xml代码。请检查 – Thejas 2014-11-22 09:45:42