简体   繁体   English

不要填写操作栏的父自定义布局

[英]don't fill parent custom layout for action bar

I have three layouts: 1. calculate_actionbar_layout.xml 2. purchasingmanager_actionbar_layout.xml 3. usermanagement_layout.xml 我有三种布局:1.calculate_actionbar_layout.xml 2.purchasemanager_actionbar_layout.xml 3.usermanagement_layout.xml

When I select a tab from action bar the relate layout for this tab with item show in action bar.but not fill custom layout in action bar. 当我从操作栏选择一个选项卡时,此选项卡的相关布局将在操作栏中显示,但不会在操作栏中填充自定义布局。

Image of my app 我的应用程式图片

Code of mainactivity: 主要活动代码:

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

On selected tab change action bar: 在选定的选项卡上,更改操作栏:

@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());
}

custom layout: 自定义布局:

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

change onTabSelected to this: you should define LinearLayout.LayoutParams to your custom layout actionbar 将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());
}

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

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