标签图标和文本都使用 android 设计支持库 [英] Tab icon and text both using android design support library

查看:22
本文介绍了标签图标和文本都使用 android 设计支持库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

]1]1我使用了 android 设计支持 tablayout 并且我在选项卡上同时获得了图标和文本..我使用了 customtabview 来垂直对齐图标和文本..我想改变选择/取消选择时的图标和文本.

]1]1i used android design support tablayout and i got both icon and text on tab .. i used a customtabview to align icon and text vertically..And i want to change the color of the icon and text when selected/unselected.

我为不同的选项卡图标制作了选择器文件..但问题是当我运行项目时,第一个默认选项卡未被选中我希望它们被默认选中..如果我转到其他选项卡然后滑动回第一个标签它工作得很好..主要问题是我无法选择第一个标签片段..请帮助我,因为我过去 2 天卡住了..我不想使用 github 中的任何库

i made selector file for different tab icon..but the thing is when i run the project the first default tab is unselected i want them to get selected by default..and if i go to other tab and then swipe back to the first tab it works perfectly fine..the main problem is i cant make the first tab fragment selected..please help me as i m stuck for past 2 days..and i dont want to use any library from github

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;
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.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.widget.TextView;

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



public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private int[] tabIcons = {
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3
    };

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

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

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

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

    private void setupTabIcons() {

        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText("ONE");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image1, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("TWO");
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image2, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);

        TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabThree.setText("THREE");
        tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image3, 0, 0);
        tabLayout.getTabAt(2).setCustomView(tabThree);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new OneFragment(), "ONE");
        adapter.addFrag(new TwoFragment(), "TWO");
        adapter.addFrag(new ThreeFragment(), "THREE");
        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);
        }
    }
}

image1.xml

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/x1" android:state_selected="true" />
        <item android:drawable="@drawable/x2" />
    </selector>

像上面一样为三个不同的图像添加三个文件.

crested three file like above for three different image.

custom_tab.xml

custom_tab.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tab"
    android:textColor="@color/selector"
    android:textSize="@dimen/tab_label"
    android:fontFamily="@string/font_fontFamily_medium"/>

文本的selector.xml

selector.xml for text

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:color="#fe5d55" /> <!-- pressed -->
    <item android:state_pressed="true"
        android:color="#fe5d55" /> <!-- focused -->
    <item android:color="#000000" /> <!-- default -->
</selector>

有人可以帮忙吗?

推荐答案

未选中是什么意思.您能否分享一下您正在努力实现的目标以及您现在所处的位置.

What do you mean by unselected. Can you share an image of what you are trying to achieve and where you are at now..

我不推荐你的方法.它做了很多不需要解决你的问题的事情.我建议使用 TabLayout 类中的图标和文本,只需设置您的图标(带有状态的可绘制对象)和文本.或者,如果需要,甚至可以使用自定义布局,但要使用 TabLayout 中的 text1 和图标.你这样做有什么问题吗?

I wouldn't recommend your approach. It does a lot of things which are not needed to solve your problem. I suggest making use of the icon and text from the TabLayout class and just set your icons(drawables with states) and text. Or, even a custom layout if required, but making use of the text1 and icon from the TabLayout. Do you have any issues doing this?

更新..

试试下面的方法:

IconTextTabLayout.java:

IconTextTabLayout.java:

package example.customtabs;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.util.AttributeSet;

import sample.customtabs.example.com.customtabs.R;


public class IconTextTabLayout extends TabLayout {


    public IconTextTabLayout(Context context) {
        super(context);
    }

    public IconTextTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IconTextTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setTabsFromPagerAdapter(@NonNull PagerAdapter adapter) {
        this.removeAllTabs();
        int i = 0;
        for (int count = adapter.getCount(); i < count; ++i) {
            this.addTab(this.newTab().setCustomView(R.layout.custom_tab)
                    .setIcon(YourIcons[i])
                    .setText(adapter.getPageTitle(i)));
        }
    }
}

custom_tab.xml

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <ImageView
        android:id="@android:id/icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

在您的活动 xml 中使用 IconTextTabLayout.喜欢:

In Your activity xml use IconTextTabLayout. Like :

<?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"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <example.customtabs.IconTextTabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white"/>

</LinearLayout>

此外,在您的活动中将视图寻呼机设置为:

also, set view pager to it in your activity like :

IconTextTabLayout tabLayout = (IconTextTabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);

在您的 viewpager 适配器中覆盖 getPageTitle 以提供标题.

Override getPageTitle in your viewpager adapter to provide title.

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

此外,如果您愿意,您也可以通过编写一个返回可绘制对象的方法从适配器中获取图标,就像 getPageTitle 方法一样.那是可选的.只是设计问题.

Additionally, if you want you can get icons too from the adapter by writing a method that returns drawables, just like the getPageTitle method. That's optional. Just a matter of design.

--

YourIcons[] = 这是一个可绘制的数组.在您的情况下 - xml 文件,带有选定和未选定的图像...

YourIcons[] = This is an array of drawables. In your case - xml files, with selected and non-selected images...

这篇关于标签图标和文本都使用 android 设计支持库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆