简体   繁体   中英

Android ActionBar Tab Layout Not Full Width

I'm having some issues with the Actionbar tab layout. For some the tabs do not stretch across the full width of the device which is weird because after trawling through at least 10 examples all 10 had centered Actionbar's the full width of their devices. Perhaps it has something to do with testing on a tablet I'm not sure. Here's the an image of the problem as well as my code.

Thanks.

Container:

public class ContainerActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tab = actionBar.newTab()
                       .setText("Mat")
                       .setTabListener(new TabListener<MatchesFragment>(
                               this, "Match", MatchesFragment.class));
    actionBar.addTab(tab);
    ...

The Container xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

Fragment:

public class MatchesFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.activity_matches,       container, false);
    return myFragmentView;
}

Fragment xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="fill_horizontal"
android:orientation="vertical" 
android:gravity="center">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/report_tab" />

</LinearLayout>

Try using a Global Layout Listener:

declare:

int requestLayoutCounter = 0;

inside onCreateView (rootView is the main View):

rootView.getViewTreeObserver().addOnGlobalLayoutListener(new MyGlobalListenerClass());

Give some id to the container:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

then declare the OnGlobalLayoutListener for the rootView inside the Fragment:

private class MyGlobalListenerClass implements ViewTreeObserver.OnGlobalLayoutListener {
    @Override
    public void onGlobalLayout() {
        View v = (View) rootView.findViewById(R.id.container);

        Display display = this.getActivity().getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int[] dimension = new int[2];
        dimension[0] = size.x; //width of the screen
        dimension[1] = size.y; //height

        v.getLayoutParams().width = (displaySize[0]);
        if (requestLayoutCounter < 25){ //weird code but works for me on different devices
            topLayout.requestLayout();
            requestLayoutCounter++;
        }
    }
}

this code will change the width of the container after the layout has been inflated, or after you change the content or orientation, let me know if it worked for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM