简体   繁体   English

Android ActionBar标签布局不完整宽度

[英]Android ActionBar Tab Layout Not Full Width

I'm having some issues with the Actionbar tab layout. 我在Actionbar标签布局中遇到了一些问题。 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. 对于一些标签不会伸展整个设备的宽度,这很奇怪,因为在遍历至少10个示例之后,所有10个都将Actionbar的中心设置为其设备的整个宽度。 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:

<?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:

<?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): 在onCreateView里面(rootView是主视图):

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

Give some id to the container: 给容器一些id:

<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: 然后为Fragment中的rootView声明OnGlobalLayoutListener:

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 此代码将在布局膨胀后更改容器的宽度,或者在更改内容或方向后,让我知道它是否适合您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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