我使用普通tabLayout
和viewpager
。我正在使用tabLayout
的自定义标签。但是一些奇怪的行为发生在标签中。
正如您所看到的,前两个标签之间有空格,但第二个和第三个标签之间没有空格。
注意:在第三个标签中,我已将箭头imageview
的可见性设置为已消失。 这不是问题。
我的tablayout屏幕布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="center"
app:tabIndicatorHeight="3dp"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
android:layout_marginTop="@dimen/margin_15"
app:tabIndicatorColor="@color/colorTransperent"
app:tabMode="fixed"
app:tabTextAppearance="?android:attr/textAppearanceMedium" />
<com.smartcompliant.views.CustomPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
自定义标签:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal">
<TextView
android:id="@+id/tvTabTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="@tools:sample/lorem"
android:textSize="@dimen/font_14"/>
<ImageView
android:id="@+id/ivArrow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_right"/>
</LinearLayout>
在java文件中填充标签,例如:
View v = LayoutInflater.from(getContext()).inflate(R.layout.cust_tabs, null);
tabForm = (TextView) v.findViewById(R.id.tvTabTitle);
tabForm.setText(getResources().getString(R.string.form));
tabForm.setTextColor(getContext().getResources().getColor(R.color.colorGreen));
tabLayout.getTabAt(0).setCustomView(v);
所以基本上有两个问题:
如果需要任何代码,请告诉我。谢谢:))
答案 0 :(得分:1)
已编辑要使固定的tabMode正常工作,您需要使imageview不可见而不会消失。
GONE
不会在您的UI层次结构中占用任何空间,其中INVISIBLE
仍将存在于UI层次结构中但不可见。在原始实现中,您可以根据您的逻辑
GONE => INVISIBLE
MainActivity.java
package in.silentsudo.shoppingcardnavigation;
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.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
private ViewPager viewPager;
private TabLayout tabLayout;
private String[] titles = new String[]{
"File details", "Select Photo", "Submit"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tab_layout);
viewPager = findViewById(R.id.view_pager);
final FormDetailsViewAdapter adapter = new FormDetailsViewAdapter(getSupportFragmentManager());
adapter.addPage(new PlaceHolderFragment());
adapter.addPage(new PlaceHolderFragment());
adapter.addPage(new PlaceHolderFragment());
viewPager.setAdapter(adapter);
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.setupWithViewPager(viewPager);
final LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < 3; i++) {
View v = inflater.inflate(R.layout.item_tab, tabLayout, false);
TextView txtView = (TextView) v.findViewById(R.id.txt);
ImageView img = (ImageView) v.findViewById(R.id.img);
txtView.setText(titles[i]);
txtView.setTextColor(ContextCompat.getColor(this, R.color.tab_color_unselected));
img.setImageResource(R.drawable.ic_right_unselected);
if(i == 2) {
img.setVisibility(View.INVISIBLE);
}
tabLayout.getTabAt(i).setCustomView(v);
}
tabLayout.getTabAt(0).select();
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(this);
tabLayout.post(new Runnable() {
@Override
public void run() {
onTabSelected(tabLayout.getTabAt(0));
}
});
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
System.out.println("Tab selected : " + tab.getPosition());
View customView = tab.getCustomView();
TextView txtView = (TextView) customView.findViewById(R.id.txt);
ImageView img = (ImageView) customView.findViewById(R.id.img);
txtView.setTextColor(ContextCompat.getColor(this, R.color.tab_color_selected));
img.setImageResource(R.drawable.ic_right_selected);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
System.out.println("Tab unselected : " + tab.getPosition());
View customView = tab.getCustomView();
TextView txtView = (TextView) customView.findViewById(R.id.txt);
ImageView img = (ImageView) customView.findViewById(R.id.img);
txtView.setTextColor(ContextCompat.getColor(this, R.color.tab_color_unselected));
img.setImageResource(R.drawable.ic_right_unselected);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
System.out.println("Tab reselected : " + tab.getPosition());
}
}
class FormDetailsViewAdapter extends FragmentStatePagerAdapter {
private List<Fragment> list;
public FormDetailsViewAdapter(FragmentManager fm) {
super(fm);
list = new ArrayList<>();
}
public void addPage(Fragment fragment) {
list.add(fragment);
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
@Override
public CharSequence getPageTitle(int position) {
return "Fragment " + position;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="in.silentsudo.shoppingcardnavigation.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentBottom="true"
android:background="#ffffff"
app:tabGravity="fill"
app:tabIndicatorColor="@android:color/transparent"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent" />
</LinearLayout>
item_tab.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:layout_gravity="center"
android:gravity="center"
android:padding="4dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="This is text" />
<ImageView
android:id="@+id/img"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginStart="8dp"
android:src="@drawable/ic_right_selected" />
</LinearLayout>
PlaceHolderFragment.java
public class PlaceHolderFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_fragment_place_holder, container, false);
}
}
fragment_place_holder.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is place holder fragment" />
</RelativeLayout>
ic_right_selected.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="18dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="18dp">
<path android:fillColor="#1DE9B6" android:pathData="M8.59,16.34l4.58,-4.59 -4.58,-4.59L10,5.75l6,6 -6,6z"/>
</vector>
ic_right_unselected.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="18dp"
android:height="18dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#BDBDBD"
android:pathData="M8.59,16.34l4.58,-4.59 -4.58,-4.59L10,5.75l6,6 -6,6z" />
</vector>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="tab_color_selected">#1DE9B6</color>
<color name="tab_color_unselected">#BDBDBD</color>
</resources>
答案 1 :(得分:1)
试试这个
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="center"
app:tabIndicatorHeight="3dp"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
android:layout_marginTop="15dp"
app:tabIndicatorColor="@android:color/transparent"
app:tabMode="scrollable"
app:tabTextAppearance="?android:attr/textAppearanceMedium" />
自定义标签布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/tvTabTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
tools:text="@tools:sample/lorem" />
<ImageView
android:id="@+id/ivArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_right" />
</LinearLayout>
设置标签图标
public void setTab(){
View v = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
TextView tabForm = (TextView) v.findViewById(R.id.tvTabTitle);
tabForm.setText("Fill details");
tabLayout.getTabAt(0).setCustomView(v);
View v2 = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
TextView tabForm2 = (TextView) v2.findViewById(R.id.tvTabTitle);
tabForm2.setText("Select photo");
tabLayout.getTabAt(1).setCustomView(v2);
View v3 = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
TextView tabForm3 = (TextView) v3.findViewById(R.id.tvTabTitle);
tabForm3.setText("submit");
ImageView imageView=v3.findViewById(R.id.ivArrow);
imageView.setVisibility(View.INVISIBLE);
tabLayout.getTabAt(2).setCustomView(v3);
}