3

我在操作栏选项卡布局方面遇到了一些问题。对于某些选项卡来说,这很奇怪,因为在浏览了至少 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);
    ...

容器 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>
4

1 回答 1

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

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

于 2014-09-22T19:14:22.643 回答