不要为操作栏填充父自定义布局

时间:2014-11-20 09:19:20

标签: android-actionbar android-custom-view fill-parent

我有三种布局:1。calculate_actionbar_layout.xml 2. purchasingmanager_actionbar_layout.xml 3. usermanagement_layout.xml

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

Image of my app

主要活动代码:

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>

1 个答案:

答案 0 :(得分:1)

更改onTabSelected to this:您应该将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());
}