2017-01-05 57 views
2

我已經使用了一個自定義的工具欄。內部自定義工具欄我有一個textView上方的tabs.Now我想設置關閉TextView的第二和第三tabs.This是我的佈局根據標籤更改文字的可見性並設置標籤的背景顏色

screenshot 在這種圖像時,正在進行的選項卡被選擇的標籤上方的文本字段的一個圖像(正在進行調查逢......... GMT)應該是可見的,而對於標籤的其餘部分,textview的可見性應該消失。我該怎麼做?

接下來的問題是我已經將整個佈局的背景設置爲在父佈局中使用背景圖像。但是我得到的默認背景是tabs.What我想要刪除默認選項卡背景顏色並獲得背景如何做到這一點?

這是我爲我的佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:background="@drawable/innerpg_bg" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#42474b" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 
      <ImageView 
       android:id="@+id/toolbarlogo" 
       android:src="@mipmap/toolbarlogo" 
       android:layout_width="wrap_content" 
       android:layout_marginRight="200dp" 
       android:layout_height="46dp" 
       /> 
     </android.support.v7.widget.Toolbar> 

     <TextView 
      android:id="@+id/deadline" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:background="#494e51" 
      android:gravity="center_vertical" 
      android:paddingLeft="40dp" 
      android:textSize="10dp" 
      android:textColor="#fff" 
      android:text="The Ongoing survey closes on Dec 31,2016 at 2:00pm GMT" 
      /> 
     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabGravity="fill" 
      app:tabMode="fixed" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
</android.support.design.widget.CoordinatorLayout> 

這裏是我的java類

package com.example.user.timothysurvey.activity; 

import android.app.Dialog; 
import android.content.Intent; 
import android.graphics.Color; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.content.ContextCompat; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 

import com.example.user.timothysurvey.R; 

import java.util.ArrayList; 
import java.util.List; 

import fragment.Ongoing; 
import fragment.Result; 
import fragment.Upcomming; 

public class MainActivity extends AppCompatActivity { 

    private Toolbar toolbar; 
    TextView deadLine; 
    private TabLayout tabLayout; 
    private ViewPager viewPager; 
    private int[] tabIcons = { 
      R.mipmap.ongoing, 
      R.mipmap.upcoming, 
      R.mipmap.results 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     deadLine= (TextView) findViewById(R.id.deadline); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     toolbar.setPadding(0, getStatusBarHeight(), 0, 0); 

     viewPager = (ViewPager) findViewById(R.id.viewpager); 
     setupViewPager(viewPager); 

     tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setSelectedTabIndicatorHeight(0); 

     tabLayout.setupWithViewPager(viewPager); 
     setupTabIcons(); 


    } 

    private void setupTabIcons() { 
     LayoutInflater inflater = LayoutInflater.from(this); 

     View view1 = inflater.inflate(R.layout.custom_tabs, null, false); 
     ((TextView) view1.findViewById(R.id.text)).setText("Ongoing"); 
     ((ImageView) view1.findViewById(R.id.image)).setImageResource(tabIcons[0]); 


     View view2 = inflater.inflate(R.layout.custom_tabs, null, false); 
     ((TextView) view2.findViewById(R.id.text)).setText("Upcomming"); 
     ((ImageView) view2.findViewById(R.id.image)).setImageResource(tabIcons[1]); 

     View view3 = inflater.inflate(R.layout.custom_tabs, null, false); 
     ((TextView) view3.findViewById(R.id.text)).setText("Results"); 
     ((ImageView) view3.findViewById(R.id.image)).setImageResource(tabIcons[2]); 

     tabLayout.getTabAt(0).setCustomView(view1); 
     tabLayout.getTabAt(1).setCustomView(view2); 
     tabLayout.getTabAt(2).setCustomView(view3); 
    } 

    private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     adapter.addFrag(new Ongoing(), "Ongoing"); 
     adapter.addFrag(new Upcomming(), "Upcomming"); 
     adapter.addFrag(new Result(), "Result"); 
     viewPager.setAdapter(adapter); 
    } 





    public int getStatusBarHeight() { 
     int result = 0; 
     int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); 
     if (resourceId > 0) { 
      result = getResources().getDimensionPixelSize(resourceId); 
     } 
     return result; 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
      case R.id.settings: 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    class ViewPagerAdapter extends FragmentPagerAdapter { 
     private final List<Fragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

     public ViewPagerAdapter(FragmentManager manager) { 
      super(manager); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return mFragmentList.get(position); 
     } 

     @Override 
     public int getCount() { 
      return mFragmentList.size(); 
     } 

     public void addFrag(Fragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return mFragmentTitleList.get(position); 
     } 
    } 
} 

回答

2

爲ViewPager實施OnPageChangeListener

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{ 

覆蓋使用onPageSelected方法

@Override 
public void onPageSelected(int position) { 

    //This method will be invoked when a new page becomes selected.set your visiblity here with respect to tab 
    if(position == 0){ 
     deadLine.setVisibility(View.VISIBLE); 
    } 
    else if(position == 1){ 
     deadLine.setVisibility(View.GONE); 
    } 
    else if(position == 2){ 
     deadLine.setVisibility(View.GONE); 
    } 

} 

@Override 
public void onPageScrollStateChanged(int state) { 

} 

@Override 
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

} 

獲取默認背景背後的tabs.add透明背景到AppBarLayout。

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#00000000" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">