0

是否有人知道如何從充氣佈局隱藏組件,一個XML組件? 我有以下代碼:隱藏充氣佈局

View tabLayout = LayoutInflater.from(this).inflate(R.layout.tab_layout, null); 
FrameLayout profile_tab_selected_indicator = 
     (FrameLayout)tabLayout.findViewById(R.id.profile_tab_selected_indicator); 

,當我試圖通過隱藏它:

profile_tab_selected_indicator.setVisibility(View.GONE); 

這是行不通的。它看起來像我錯過了一些東西,或者不可能從膨脹的佈局中隱藏xml組件。我也試圖通過LayoutParams寬度和高度設置爲0dp,並沒有正常工作。

onCreate()這樣外部的自定義函數:

public static View tabLayout; 

protected void onCreate(){ 
    //some code here 
buildTabLayout(); 
} 

public void buildTabLayout(){ 
    tabLayout = LayoutInflater.from(this).inflate(R.layout.tab_layout, null); 
} 

然後我想在這裏隱藏:

public void onTabChanged(String tabId) { 
    FrameLayout profile_tab_selected_indicator = (FrameLayout)tabLayout.findViewById(R.id.profile_tab_selected_indicator); 

    profile_tab_selected_indicator.setVisibility(View.GONE); 
} 

這裏的充氣佈局的XML代碼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/profile_tab_bar_bg" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/profile_top_bar_shadow_xml" > 
    </FrameLayout> 

    <ImageView 
     android:id="@+id/iv_tabIcon" 
     android:layout_width="30dp" 
     android:layout_height="30dp" /> 

    <TextView 
     android:id="@+id/tv_tabTitle" 
     style="@style/ProfileTabLabelsStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@drawable/profile_tab_label_selector" /> 

    <FrameLayout 
     android:id="@+id/profile_tab_selected_indicator" 
     android:layout_width="match_parent" 
     android:layout_height="2dp" 
     android:background="@color/ProfileTabSelected" > 
    </FrameLayout> 

</LinearLayout> 

編輯:

所有的代碼負責我的標籤佈局:

// tabs labels 
    public static String[] tabsTitles = new String[] { "PROFILE", "CAMPAIGNS", 
      "STATISTICS" }; 

    public static View tabLayout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.vw_profile); 

     // initializing xml components 
     init(); 

     // Tab Layut builder 
     buildTabLayout(); 


    } 
private void buildTabLayout() { 
     // TODO Auto-generated method stub 
     Resources res = getResources(); // Resource object to get Drawables 
     TabHost tabHost = getTabHost(); // The activity TabHost 
     tabHost.setOnTabChangedListener(this); 
     TabHost.TabSpec spec; // Resusable TabSpec for each tab 
     View tabLayout; // tabLayout 
     Intent intent; // Reusable Intent for each tab 

     // PROFILE TAB 
     tabLayout = createTabLayout(this, tabsTitles[0]); 
     intent = new Intent().setClass(this, VieweedsProfileTabActivity.class); 
     spec = tabHost.newTabSpec(tabsTitles[0]).setIndicator(tabLayout) 
       .setContent(intent); 
     tabHost.addTab(spec); 

     // CAMPAIGNS TAB 
     tabLayout = createTabLayout(this, tabsTitles[1]); 
     intent = new Intent() 
       .setClass(this, VieweedsCampaignsTabActivity.class); 
     spec = tabHost.newTabSpec(tabsTitles[1]).setIndicator(tabLayout) 
       .setContent(intent); 
     tabHost.addTab(spec); 

     // STATISTICS TAB 
     tabLayout = createTabLayout(this, tabsTitles[2]); 
     intent = new Intent().setClass(this, 
       VieweedsStatisticsTabActivity.class); 
     spec = tabHost.newTabSpec(tabsTitles[2]).setIndicator(tabLayout) 
       .setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(0); 

    } 

    public void onTabChanged(String tabId) { 
     // TODO Auto-generated method stub 
     FrameLayout profile_tab_selected_indicator = (FrameLayout)tabLayout.findViewById(R.id.profile_tab_selected_indicator); 
     FrameLayout campaigns_tab_selected_indicator = (FrameLayout)tabLayout.findViewById(R.id.campaigns_tab_selected_indicator); 

     if (tabId.equals(tabsTitles[0])) { 
      // profile tab 
      Toast.makeText(this, tabId, Toast.LENGTH_LONG).show(); 
      profile_tab_selected_indicator.setVisibility(View.VISIBLE); 
      campaigns_tab_selected_indicator.setVisibility(View.GONE); 
     } else if (tabId.equals(tabsTitles[1])) { 
      // campaigns tab 
      Toast.makeText(this, tabId, Toast.LENGTH_LONG).show(); 
      profile_tab_selected_indicator.setVisibility(View.GONE); 
      campaigns_tab_selected_indicator.setVisibility(View.VISIBLE); 
     } else { 
      Toast.makeText(this, tabId, Toast.LENGTH_LONG).show(); 
      // statistics tab 

     } 
    } 

    // creating tab layout for each tab 
    public static View createTabLayout(Context c, String tabTitleText){ 
     tabLayout = LayoutInflater.from(c).inflate(R.layout.tab_layout, null); 
     TextView tabTitle = (TextView)tabLayout.findViewById(R.id.tv_tabTitle); 
     tabTitle.setTypeface(arial); 
     ImageView tabIcon = (ImageView)tabLayout.findViewById(R.id.iv_tabIcon); 
     tabTitle.setText(tabTitleText); 

     //assigning tab icons for each tab 
     if(tabTitleText.equals(tabsTitles[0])){ 
      //profile tab 
      tabIcon.setImageResource(R.drawable.profile_tab_bar_profile_tab_icon); 
     }else if(tabTitleText.equals(tabsTitles[1])){ 
      //campaigns tab 
      tabIcon.setImageResource(R.drawable.profile_tab_bar_campaigns_tab_icon); 
     }else{ 
      //statistics tab 
      tabIcon.setImageResource(R.drawable.profile_tab_bar_statistics_tab_icon); 
     } 

     return tabLayout; 
    } 
+0

請提供您的加入你的代碼中的tabLayout? –

+0

您應該提供一個xml的佈局。乍一看代碼很好 –

+0

我用xml代碼更新了這個問題。謝謝! –

回答

0

你做一些事情錯在你的代碼。我不知道爲什麼你選擇輸出使tabLayout場在你的類,但這樣做tabLayout將指向您設置(一個爲統計片)的最後一個標籤指標。因爲tabLayout僅指向最後一個選項卡,在onTabChanged回調什麼都不會發生(因爲你沒有在最後一個選項卡做任何事情)。

我勸你改變你onTabChanged方法和搜索直接與getChildTabViewAt標籤視圖,以確保你在正確的地方尋找那個FrameLayout

public void onTabChanged(String tabId) { 
     // the first tab view 
     View tab1 = getTabWidget().getChildTabViewAt(0); 
     FrameLayout profile_tab_selected_indicator = (FrameLayout)tab1.findViewById(R.id.profile_tab_selected_indicator); 
     // the second tab view 
     View tab2 = getTabWidget().getChildTabViewAt(1); 
     // where does this come from?!?! it is in the second tab?!?! 
     FrameLayout campaigns_tab_selected_indicator = (FrameLayout)tab2.findViewById(R.id.campaigns_tab_selected_indicator); 
     // the third tab view 
     View tab3 = getTabWidget().getChildTabViewAt(2);    

     if (tabId.equals(tabsTitles[0])) { 
      // profile tab 
      Toast.makeText(this, tabId, Toast.LENGTH_LONG).show();      
      profile_tab_selected_indicator.setVisibility(View.VISIBLE); 
      campaigns_tab_selected_indicator.setVisibility(View.GONE); 
     } else if (tabId.equals(tabsTitles[1])) { 
      // campaigns tab 
      Toast.makeText(this, tabId, Toast.LENGTH_LONG).show(); 
      profile_tab_selected_indicator.setVisibility(View.GONE); 
      campaigns_tab_selected_indicator.setVisibility(View.VISIBLE); 
     } else { 
      Toast.makeText(this, tabId, Toast.LENGTH_LONG).show(); 
      // statistics tab 

     } 
    }