2015-11-11 10 views
14

के साथ कस्टम दृश्यों का उपयोग करते समय टैब से पैडिंग को हटा नहीं सकता है मैंने कस्टम व्यू में एक सापेक्ष लेआउट जोड़ा है और इसे टैब लेआउट में जोड़ा है। मैं टैब के लिए एक सफेद पृष्ठभूमि का उपयोग कर रहा हूं और टैब कस्टम लेआउट में कोई पैडिंग लागू नहीं किया है। लेकिन फिर भी मुझे टैब पर पैडिंग मिल रही है जिसके कारण मुझे टैब के भीतर कुछ भूरे रंग की पृष्ठभूमि दिखाई दे रही है जैसे एंड्रॉइड आंतरिक रूप से टैब पर पैडिंग लागू कर रहा है। मैं तीन टेक्स्ट दृश्यों को प्रदर्शित करने की कोशिश कर रहा हूं जो मैं करने में सक्षम हूं लेकिन उनमें से एक टैब पर लागू पैडिंग के कारण छंटनी कर रहा है। इसके अलावा मैं टैब के लिए लगातार पृष्ठभूमि रंग रखना चाहता हूं।टैब लेआउट

activity_home.xml 

<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.customtablayoutapplication.HomeActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 


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

    <include layout="@layout/content_home" /> 


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



fragment_home.xml 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:theme="@style/AppTheme.AppBarOverlay" 
    tools:context="com.customtablayoutapplication.HomeFragment" 
    tools:showIn="@layout/activity_home"> 

    <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.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" /> 

</LinearLayout> 


custom_tab.xml`enter code here` 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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="@android:color/white" 
    android:layout_height="match_parent" 
    android:layout_gravity="center"> 

    <TextView 
     android:id="@+id/total_request_count" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="12" 
     android:textAllCaps="false" 
     android:visibility="gone" 
     android:textColor="@color/colorPrimaryDark" 
     android:textSize="12sp" /> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerVertical="true" 
     android:fitsSystemWindows="true" 
     android:gravity="center"> 

     <TextView 
      android:id="@+id/request_status" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="New Request" 
      android:textAllCaps="false" 
      android:textColor="@color/colorPrimaryDark" 
      android:textSize="12sp" /> 

     <TextView 
      android:id="@+id/badge_icon" 
      android:layout_width="13dp" 
      android:layout_height="13dp" 
      android:layout_alignParentRight="true" 
      android:layout_alignTop="@id/request_status" 
      android:layout_marginLeft="-3dp" 
      android:layout_marginTop="15dp" 
      android:background="@drawable/circular_text_background" 
      android:clickable="false" 
      android:visibility="gone" /> 
    </RelativeLayout> 


</RelativeLayout> 


HomeFragment.java 

package com.customtablayoutapplication; 

import android.os.Bundle; 
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.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

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

/** 
* A placeholder fragment containing a simple view. 
*/ 
public class HomeFragment extends Fragment { 

    private ViewPager viewPager; 
    private TabLayout tabLayout; 

    public HomeFragment() { 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_home, container, false); 
     viewPager = (ViewPager) view.findViewById(R.id.viewpager); 
     setupViewPager(viewPager); 

     tabLayout = (TabLayout) view.findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 
     setUpTabIcons(); 
     return view; 
    } 

    private void setUpTabIcons() { 
     RelativeLayout tabNewRequest= (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null); 
     TextView tabNewRequesttxt = (TextView) tabNewRequest.findViewById(R.id.request_status); 
     tabNewRequesttxt.setText("New Request"); 
     tabLayout.getTabAt(0).setCustomView(tabNewRequest); 

     RelativeLayout tabInProgress= (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null); 
     TextView tabInProgresstxt = (TextView) tabInProgress.findViewById(R.id.request_status); 
     tabInProgresstxt.setText("In Progress"); 
     tabLayout.getTabAt(1).setCustomView(tabInProgress); 

     RelativeLayout tabWorkDone= (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null); 
     TextView tabWorkDonetxt = (TextView) tabWorkDone.findViewById(R.id.request_status); 
     tabWorkDonetxt.setText("Work Done"); 
     tabLayout.getTabAt(2).setCustomView(tabWorkDone); 

     RelativeLayout tabDelivered= (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null); 
     TextView tabDeliveredtxt = (TextView) tabDelivered.findViewById(R.id.request_status); 
     tabDeliveredtxt.setText("Delivered"); 
     tabLayout.getTabAt(3).setCustomView(tabDelivered); 
    } 

    private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager()); 
     adapter.addFragment(new NewRequestFragment(), "New Request"); 
     adapter.addFragment(new InProgressFragment(), "In Progress"); 
     adapter.addFragment(new WorkDoneFragment(), "Work Done"); 
     adapter.addFragment(new DeliveredFragment(), "Delivered"); 
     viewPager.setAdapter(adapter); 
    } 

    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 addFragment(Fragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 

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

Fixed Tab are having grey background arround white background

+0

मैं TabLayout के आंतरिक स्रोत कोड को देखा और यह वास्तव में paddingStart उपयोग कर रहा है, PaddingEnd। आदि कृपया सुझाव दें कि मैं पैडिंग को कैसे हटा सकता हूं। उसका लिंक मेरे मुद्दे का स्क्रीन शॉट है: http://i.stack.imgur.com/Zqx1U.png – user3492435

उत्तर

30
<android.support.design.widget.TabLayout 
      android:id="@+id/tab_layout" 
      android:layout_width="210dp" 
      android:layout_height="28dp" 
      android:layout_centerInParent="true" 
      android:background="@drawable/bg_forum_tab" 
      app:tabIndicatorColor="@color/colorBtnBg" 
      app:tabIndicatorHeight="0dp" 
      app:tabPaddingBottom="-1dp" 
      app:tabPaddingEnd="-1dp" 
      app:tabPaddingStart="-1dp" 
      app:tabPaddingTop="-1dp" 
      app:tabSelectedTextColor="@color/colorBtnBg" 
      app:tabTextColor="@color/colorWhite" /> 

सेट tabPaddingStart/tabPaddingEnd/tabPaddingTop/tabPaddingBottom इस तरह:

यहाँ मेरी लेआउट कोड है।

+3

मैंने इस समाधान की कोशिश की लेकिन मेरी मदद नहीं की ... अभी भी – Min2

+0

यह मेरे लिए जवाब था! धन्यवाद –

+3

मुझे पता चला है कि 'tabPaddingTop/tabPaddingBottom' केवल तभी काम करता है जब' कस्टम व्यू 'का रूट व्यू' रिलेटिव लयआउट 'होता है। 'लीनियरलाउट' और 'फ्रैगेटलायआउट' के साथ यह आश्चर्यजनक रूप से काम नहीं करता है। और पैडिंग '-1dp' –

24

यह मेरे लिए काम किया:

में अपने TabLayout आप 0dp को tabPaddingEnd और tabPaddingStart सेट करना होगा।

अपने कस्टम व्यू में आपको अपने कस्टम रंग के साथ सभी जगहों को भरने के लिए layout_width और layout_height से match_parent पर सेट करना होगा।

TabLayout:

<android.support.design.widget.TabLayout 
    android:id="@+id/tl_dashboard" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:layout_alignParentBottom="true" 
    app:tabGravity="fill" 
    app:tabIndicatorColor="@color/colorRed" 
    app:tabMode="fixed" 
    app:tabPaddingStart="0dp" 
    app:tabPaddingEnd="0dp"/> 

और कस्टम दृश्य:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
    <!--Your widgets-->   
</RelativeLayout> 
+0

धन्यवाद, यह बहुत अच्छा काम कर रहा है –

+0

अच्छा समाधान .. – Steve

2

मैं शून्य करने के लिए अपने कस्टम दृश्य की मूल के मार्जिन & गद्दी की स्थापना, जब टैब लेआउट के लिए नए टैब जोड़कर इस हल किया।

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    lp.setMargins(0,0,0,0); 

    TabLayout.Tab newTab = mTabsBottom.newTab().setCustomView(R.layout.view_custom_tab); 
    setupTabParentLayout(newTab.getCustomView(), lp); 
    .. 
    .. 
    .. 
    private void setupTabParentLayout(View customView, LinearLayout.LayoutParams lp) 
    { 
     LinearLayout tabParent = (LinearLayout) customView.getParent(); 
     tabParent.setLayoutParams(lp); 
     tabParent.setPadding(0,0,0,0); 
    } 

ट्रिक यहां कस्टम व्यू के माता-पिता के लिए LinearLayout.LayoutParams का उपयोग करना था। अगले समाधान

app:tabPaddingEnd="0dp" 
app:tabPaddingStart="0dp" 

सूचना, इस तरह से ऊपर और नीचे paddings के लिए काम करते doesn `t, लेकिन मुझे लगता है कि:

+0

इससे मेरी मदद मिली। –

4

एक्सएमएल के माध्यम से आप इस तरह केवल बाएँ और दाएँ paddings को हटा सकते हैं जब आप टैब आइटम के रूप में कस्टम दृश्यों का उपयोग करते हैं, तो आपको लेआउटपार्म को देखने और सेट पैडिंग सेट करने की आवश्यकता है।

for (int i = 0; i < tabLayout.getTabCount(); i++) {   
     View tabView = LayoutInflater.from(context) 
       .inflate(LayoutInflater.from(this), R.layout.item_main_tabview, null, false); 

     tabView.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
     tabView.setPadding(0, 0, 0, 0); 
     tabLayout.getTabAt(i).setCustomView(tabViewBinding.getRoot()); 
    } 
संबंधित मुद्दे