1

我有三个布局: 1. calculate_actionbar_layout.xml 2. purchasemanager_actionbar_layout.xml 3. usermanagement_layout.xml

当我从操作栏中选择一个选项卡时,此选项卡的相关布局与操作栏中的项目显示。但不填充操作栏中的自定义布局。

我的应用程序的图像

主要活动代码:

ActionBar actionbar = getActionBar();
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayShowHomeEnabled(false);
actionbar.setDisplayShowCustomEnabled(true);
View cView = getLayoutInflater().inflate(R.layout.usermanagement_layout, null);
actionbar.setCustomView(cView);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

ImageView i=(ImageView) cView.findViewById(R.id.imageviewadduser);
i.setOnClickListener(this);

在选定的选项卡上更改操作栏:

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
final ActionBar actionbar = getActionBar();
View cView=null;
switch (tab.getPosition()) {
case 0:
cView = getLayoutInflater().inflate(R.layout.calculate_actionbar_layout, null);
 actionbar.setCustomView(cView);

//actionbar.setCustomView(R.layout.calculate_actionbar_layout);

break;
case 1:
cView = getLayoutInflater().inflate(R.layout.purchasingmanager_actionbar_layout, null);
actionbar.setCustomView(cView);

//actionbar.setCustomView(R.layout.purchasingmanager_actionbar_layout);
break;
case 2:
cView = getLayoutInflater().inflate(R.layout.usermanagement_layout, null);

actionbar.setCustomView(cView);

//actionbar.setCustomView(R.layout.usermanagement_layout);
break;
default:
break;
}
viewpager.setCurrentItem(tab.getPosition());
}

自定义布局:

<?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="horizontal"
android:background="#0d93d2"
android:weightSum="4"
android:gravity="center"
android:layout_gravity="center"
 >
<ImageView
android:id="@+id/imageviewusermanagment"
android:layout_width="0sp"
android:layout_weight="2.5"
android:layout_height="wrap_content"
android:src="@drawable/usermanagment"
android:scaleType="fitStart"
android:paddingLeft="20sp"

 />
 <ImageView
android:id="@+id/imageviewdeleteuser"
android:layout_width="0sp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:src="@drawable/deleteuser"

 />



<ImageView
android:id="@+id/imageviewedituser"
android:layout_width="0sp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:src="@drawable/edituser"/>

<ImageView
    android:id="@+id/imageviewadduser"
android:layout_width="0sp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:src="@drawable/adduser"
android:onClick="onClick"
     />

</LinearLayout>
4

1 回答 1

1

将 onTabSelected 更改为:您应该将 LinearLayout.LayoutParams 定义为您的自定义布局操作栏

public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    final ActionBar actionbar = getActionBar();
    View cView=null;
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);
    switch (tab.getPosition()) {
    case 0:

         cView = getLayoutInflater().inflate(R.layout.calculate_actionbar_layout, null);
         cView.setLayoutParams(param);
         actionbar.setCustomView(cView);

        //actionbar.setCustomView(R.layout.calculate_actionbar_layout);

        break;
    case 1:

        cView = getLayoutInflater().inflate(R.layout.purchasingmanager_actionbar_layout, null);
        cView.setLayoutParams(param);
        actionbar.setCustomView(cView);

        //actionbar.setCustomView(R.layout.purchasingmanager_actionbar_layout);
        break;
    case 2:
         cView = getLayoutInflater().inflate(R.layout.usermanagement_layout, null);
         cView.setLayoutParams(param);
        actionbar.setCustomView(cView);

        //actionbar.setCustomView(R.layout.usermanagement_layout);
        break;
    default:
        break;
    }
    viewpager.setCurrentItem(tab.getPosition());
}
于 2014-11-20T11:22:21.180 回答