2014-11-22 21 views
0

嗨我想要具有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。請幫我解決這個問題。

+0

你可以把動作條XML代碼? – 2014-11-22 09:34:52

+0

你已經把xml代碼。請檢查 – Thejas 2014-11-22 09:45:42

回答

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); 

然後檢查希望這將有助於你

+0

不,它沒有工作... – Thejas 2014-11-22 10:07:17

相關問題