与可滑动标签视图一起使用时如何使自定义操作栏填充屏幕宽度?

时间:2014-11-22 09:31:53

标签: android android-actionbar tabview

您好我想要具有可跳过标签视图的自定义操作栏。我已经创建了自定义操作栏并为动作栏设置了自定义视图。当我与其他屏幕一起使用时,操作栏效果很好但是当我使用tabview时,应用程序图标会出现在操作栏的左侧。请帮我制作自定义操作栏,以便在tabview上填充整个宽度。我的代码是这样的,

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:这是我的自定义操作栏布局,其中包含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。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

更改此代码

LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

通过

ActionBar.LayoutParams lp1 = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);

然后检查希望它会帮助你