我想在文本

时间:2017-11-17 12:56:08

标签: android

我想在文本左侧显示图标,但问题是图标显示在文本上方。它占用了更多的空间。 我想保留文本左侧的图标(标题)。我该怎么办?

主要活动如下:

的java.class

public class Tabsadar extends AppCompatActivity {
ExpandableRelativeLayout
expandableLayout1,
expandableLayout2,
expandableLayout3,
expandableLayout4;

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
        R.drawable.ic_action_name,
        R.drawable.thanatottho,
        R.drawable.proyojonio
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tabsadar);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();
}

private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new tabsadarc1(), "ফিচার");
    adapter.addFrag(new tabsadarc2(), "তথ্যাবলি");
    adapter.addFrag(new tabsadarc3(), "প্রয়োজনীয়");
    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 addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

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

   }'

我想要the required one

之类的东西 在我的情况下发生了类似的事情 what i got by uper code

任何解决方案?

2 个答案:

答案 0 :(得分:2)

根据您的要求,我建议您为标签创建自定义视图。

custom_tab_view.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"
android:orientation="horizontal">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/_6sdp"
        android:background="@drawable/ic_menu_gallery"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="@dimen/_3sdp"
        android:textColor="@drawable/tab_text_selector"
        android:textSize="@dimen/_15sdp" />
</LinearLayout>

</RelativeLayout>

这种自定义标签的布局,PagerAdapter

 //On  Create
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
        View customView = mPagerAdapter.getCustomView(getActivity(), i);
        mainTabLayout.getTabAt(i).setCustomView(customView);
    }

//Pager Adapter 
public static class ViewPagerAdapter extends SmartFragmentStatePagerAdapter {
    private List<Map<String, Object>> maps = new ArrayList<>();

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

    public View getCustomView(Context context, int pos) {
        View mView = LayoutInflater.from(context).inflate(R.layout.custom_tab_view, null);
        TextView mTextView = (TextView) mView.findViewById(R.id.textView);
        ImageView imageView = (ImageView) mView.findViewById(R.id.imageView);
        mTextView.setGravity(Gravity.CENTER);
        mTextView.setTypeface(Typeface.createFromAsset(context.getAssets(),
                "fonts/aller_regular.ttf"));
        mTextView.setText(getPageTitle(pos));
        return mView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragments[position];
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new CompletedTaskFragment();
            case 1:
                return new PendingTaskFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }
}

希望它能帮到你。

答案 1 :(得分:0)

只需使用 app:tabInlineLabel =“ true”

赞:-

 <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabInlineLabel="true"
        app:tabGravity="fill"/>