Android ActionBar标签布局不完整宽度

时间:2013-09-05 06:20:54

标签: java android layout android-fragments tablet

我在Actionbar标签布局中遇到了一些问题。对于一些标签不会伸展整个设备的宽度,这很奇怪,因为在遍历至少10个示例之后,所有10个都将Actionbar的中心设置为其设备的整个宽度。也许它与在平板电脑上进行测试有关,我不确定。这是问题的图像以及我的代码。

感谢。

容器:

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

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" >

片段:

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

片段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>

1 个答案:

答案 0 :(得分:0)

尝试使用全局布局侦听器:

声明:

int requestLayoutCounter = 0;

在onCreateView里面(rootView是主视图):

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

给容器一些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" >

然后为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++;
        }
    }
}

此代码将在布局夸大后更改容器的宽度,或者在更改内容或方向后,让我知道它是否适合您