微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

选项卡图标和文本均使用android设计支持库

02001] 1] 1i使用了android设计支持tablayout,我在选项卡上同时显示了图标和文本..我使用customtabview垂直对齐图标和文本.我想在选择/取消选择时更改图标和文本的颜色.

我为不同的选项卡图标制作了选择器文件.但事情是当我运行项目时,第一个认选项卡未被选中我希望它们认被选中..如果我转到其他选项卡然后再刷回第一个选项卡它工作得很好..主要的问题是我不能让第一个选项卡片段被选中..请帮助我,因为我坚持过去2天..我不想使用任何来自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);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab,null);
        tabTwo.setText("TWO");
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(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);
        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>

像三个不同的图像上面的三个文件.

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

<?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>

有人可以帮忙吗?

解决方法

你是什​​么意思未被选中.
你能分享一下你想要达到的目标以及你现在所处的位置.

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

更新..

试试以下内容

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

<?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.喜欢 :

<?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>

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

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

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

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

此外,如果您希望通过编写返回drawables的方法,也可以从适配器获取图标,就像getPageTitle方法一样.这是可选的.只是设计问题.

YourIcons [] =这是一系列drawables.在您的情况下 – xml文件,包含选定和未选择的图像……

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐