2

我試圖填充選項卡的視圖。有關確切的事情更多的信息,我嘗試以期實現請閱讀我剛纔的問題:How to customize individual tabs? (changing background color, indicator color and text color)RelativeLayout/customview不填充選項卡的寬度

我現在得到的結果是這樣的:enter image description here

正如你所看到的標籤沒有完全充滿查看其寬度。

我的佈局XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background ="@color/black" 
    > 

    <TextView 
     android:id="@+id/nieuws_tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="@string/nieuws" 
     android:textColor="@android:color/white" 
     android:textStyle="bold"/> 
</RelativeLayout> 

我的MainActivity:

package com.example.android.effectivenavigation; 

import android.app.ActionBar; 
import android.app.ActionBar.Tab; 
import android.app.FragmentTransaction; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class MainActivity extends FragmentActivity implements ActionBar.TabListener 
{ 
    AppSectionsPagerAdapter mAppSectionsPagerAdapter; 
    //The viewpager displays on of the section at a time 
    ViewPager mViewPager; 

    public void onCreate(Bundle savedInstanceState) 
    {  
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Create the adapter that will return a fragment for each of the three primary sections 
     // of the app. 
     mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the action bar. 
     final ActionBar actionBar = getActionBar(); 
     //set custom actionbar 
     actionBar.setCustomView(R.layout.titlebar); 
     //Displays the custom design in the actionbar 
     actionBar.setDisplayShowCustomEnabled(true); 
     //Turns the homeIcon a View  
     View homeIcon = findViewById(android.R.id.home); 
     //Hides the View (and so the icon) 
     ((View)homeIcon.getParent()).setVisibility(View.GONE);   

     // Specify that we will be displaying tabs in the action bar. 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Set up the ViewPager, attaching the adapter and setting up a listener for when the 
     // user swipes between sections. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mAppSectionsPagerAdapter); 

     mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() 
     {   
      @Override 
      public void onPageSelected(int position) 
      { 
        // When swiping between different app sections, select the corresponding tab. 
        // We can also use ActionBar.Tab#select() to do this if we have a reference to the Tab. 
        actionBar.setSelectedNavigationItem(position); 
      } 
     }); 


     // For each of the sections in the app, add a tab to the action bar. 
     for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) 
     { 
      if(i == 0) 
      { 
       //final View firstCustomView = new CustomView(this); 
       //firstCustomView.setBackgroundColor(Color.BLUE); 
       Tab tab = actionBar.newTab().setTabListener(this).setCustomView(R.layout.nieuws_tab_layout); 

       actionBar.addTab(tab); 
      } 
      else 
      { 
      // Create a tab with text corresponding to the page title defined by the adapter. 
      // Also specify this Activity object, which implements the TabListener interface, as the 
      // listener for when this tab is selected. 
      Tab tab = actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(i)).setTabListener(this);    
      actionBar.addTab(tab); 
      } 
     } 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
    { 
    } 

    @Override 
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
    { 
     //CustomView ctv; 
     //ctv = new CustomView(context, R.attr.tabStyleAttr); 
     // When the given tab is selected, switch to the corresponding page in the ViewPager. 
     //LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
     //View tabView = inflater.inflate(R.layout.nieuws_tab_layout, null); 
     //tabView.setBackgroundColor(0xFF00FF00); 
     //tab.setCustomView(tabView); 
     mViewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
    { 
    } 

    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter 
    { 
     public AppSectionsPagerAdapter(FragmentManager fm) 
     { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int i) 
     { 
      switch (i) 
      { 
       case 0: 
        // The first section of the app is the most interesting -- it offers 
        // a launchpad into the other demonstrations in this example application. 
        return new LaunchpadSectionFragment(); 

       default: 
        // The other sections of the app are dummy placeholders. 
        Fragment fragment = new DummySectionFragment(); 
        Bundle args = new Bundle(); 
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); 
        fragment.setArguments(args); 
        return fragment; 
      } 
     } 

     @Override 
     public int getCount() 
     { 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) 
     { 
      switch(position) 
      { 
       case 0: 
       { 
        return "Tab1"; 
       } 
       case 1: 
       { 
        return "Tab2"; 
       } 
       case 2: 
       { 
        return "Tab3"; 
       } 
       default: 
       { 
        return "Section " + (position + 1); 
       } 
      } 
     } 
    } 
    public static class LaunchpadSectionFragment extends Fragment 
    { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
     { 
      View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false); 

      // Demonstration of a collection-browsing activity. 
      rootView.findViewById(R.id.demo_collection_button).setOnClickListener(new View.OnClickListener() 
      { 
       @Override 
       public void onClick(View view) 
       { 
        Intent intent = new Intent(getActivity(), CollectionDemoActivity.class); 
        startActivity(intent); 
       } 
      }); 

      // Demonstration of navigating to external activities. 
      rootView.findViewById(R.id.demo_external_activity).setOnClickListener(new View.OnClickListener() 
      { 
       @Override 
       public void onClick(View view) 
       { 
        // Create an intent that asks the user to pick a photo, but using 
        // FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, ensures that relaunching 
        // the application from the device home screen does not return 
        // to the external activity. 
        Intent externalActivityIntent = new Intent(Intent.ACTION_PICK); 
        externalActivityIntent.setType("image/*"); 
        externalActivityIntent.addFlags(
        Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
        startActivity(externalActivityIntent); 
       } 
      }); 
      return rootView; 
     } 
    } 

    /** 
    * A dummy fragment representing a section of the app, but that simply displays dummy text. 
    */ 
    public static class DummySectionFragment extends Fragment 
    { 
     public static final String ARG_SECTION_NUMBER = "section_number"; 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     { 
      View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false); 
      Bundle args = getArguments(); 
      ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER))); 
      return rootView; 
     } 
    } 
    public class CustomView extends View 
    { 
     public CustomView(Context context) 
     { 
      super(context, null); 
     } 
    } 
} 
+0

試圖改變它的重力。 。 –

+0

@AdilWaqar我試過了,它不會改變任何 – Shishi

回答

1

那些是墊襯。使用下面的樣式與您的TabHost擺脫它們,或直接在您的佈局中設置android:paddingStartandroid:paddingEnd0dp

Android 4.0及更高

<style name="TabStyle" parent="@android:style/Widget.Holo.Light.ActionBar.TabView"> 
    <item name="android:paddingStart">0dip</item> 
    <item name="android:paddingEnd">0dip</item> 
</style> 

較早的Android

<style name="TabStyle" parent="@android:style/Widget.ActionBar.TabView"> 
    <item name="android:paddingStart">0dip</item> 
    <item name="android:paddingEnd">0dip</item> 
</style> 
+0

使用' 0dip'似乎適用於較低的api我正在使用api 11 – Shishi

+0

我剛剛更新了答案。對於較舊的Android版本,您需要使用不同的父級樣式。 –

1

確保您使用的是正確的DPI(華電國際,MDPI,等。)文件夾,你的背景資源的設備匹配/你正在開發的模擬器。

我和你有同樣的問題,即使我通過setCustomView()方法使用自定義選項卡,覆蓋樣式(還有支持庫),甚至從父佈局中刪除填充。

現在按我的預期工作,希望它有幫助!