Android:不推荐使用非滑动式标签页?

Android:不推荐使用非滑动式标签页?,第1张

概述我有一个非常简单的任务(至少我是这么认为的)-制作5个不可滚动的静态标签,其中包含图标和片段我花了整整一整天的时间完成这项任务,我真的感到很惊讶,似乎在现代AndroidAPI中很难实现,因为我发现的所有东西都没有用:>PagerTabStrip-可滚动,不能使用固定数量的显示标签>ActionB

我有一个非常简单的任务(至少我是这么认为的)-制作5个不可滚动的静态标签,其中包含图标和片段

我花了整整一整天的时间完成这项任务,我真的感到很惊讶,似乎在现代Android API中很难实现,因为我发现的所有东西都没有用:

> PagerTabStrip-可滚动,不能使用固定数量的显示标签
> Actionbar.TabListener-自API 21后不推荐使用
> FragmentTabHost-有一些错误“渲染期间引发异常:没有标签已知的标签为null”
> SlIDingTabLayout-再次滑动:(不能使用固定数量的显示标签

也许我错过了什么?问候.

解决方法:

如我所言,我为您做了一些演示项目.

最简单的解决方案是linearLayout,这是我使用的,也是androID的图像.

首先,您需要在values文件夹中创建一个名为attrs.xml的文件.如果您已经拥有它,那么只需添加样式

<?xml version="1.0" enCoding="utf-8"?><resources>    <declare-styleable name="SimpleTabIndicator">        <attr name="numberOfTabs" format="integer"/>        <attr name="indicatorcolor" format="color"/>    </declare-styleable></resources>

然后创建一个名为SimpleTabIndicator的类

import androID.content.Context;import androID.content.res.Resources;import androID.content.res.TypedArray;import androID.graphics.Canvas;import androID.graphics.Paint;import androID.os.Build;import androID.util.AttributeSet;import androID.vIEw.VIEw;public class SimpleTabIndicator extends VIEw {    private static final String TAG = SimpleTabIndicator.class.getSimplename();    private float density;    private int measuredHeight, measureDWIDth;    private int mNumberOfTabs;    private Paint mIndicatorPaint;    private int mIndicatorcolor = 0xFFFDE992;    private int currentTab = 1;    public SimpleTabIndicator(Context context) {        super(context);        init(null, 0);    }    public SimpleTabIndicator(Context context, AttributeSet attrs) {        super(context, attrs);        init(attrs, 0);    }    public SimpleTabIndicator(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init(attrs, defStyleAttr);    }    private voID init(AttributeSet attrs, int style) {        if (!isInEditMode() && Build.VERSION.SDK_INT >= 11) {            setLayerType(VIEw.LAYER_TYPE_NONE, null);        }        Resources res = getResources();        density = res.getdisplayMetrics().density;        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.SimpleTabIndicator, style, 0);        mIndicatorcolor = typedArray.getcolor(R.styleable.SimpleTabIndicator_indicatorcolor, mIndicatorcolor);        mNumberOfTabs = typedArray.getInt(R.styleable.SimpleTabIndicator_numberOfTabs, 1);        typedArray.recycle();        mIndicatorPaint = new Paint(Paint.ANTI_AliAS_FLAG);        mIndicatorPaint.setStyle(Paint.Style.FILL);        mIndicatorPaint.setcolor(mIndicatorcolor);    }    public int getNumberOfTabs() {        return mNumberOfTabs;    }    public voID setNumberOfTabs(int mNumberOfTabs) {        this.mNumberOfTabs = mNumberOfTabs;        invalIDate();    }    public int getCurrentTab() {        return currentTab;    }    public voID setCurrentTab(int currentTab) {        this.currentTab = currentTab;        invalIDate();    }    @OverrIDe    protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) {        measuredHeight = getDefaultSize(getSuggestedMinimumHeight(), heightmeasureSpec);        measureDWIDth = getDefaultSize(getSuggestedMinimumWIDth(), wIDthMeasureSpec);        setMeasuredDimension(measureDWIDth, measuredHeight);    }    @OverrIDe    protected voID onDraw(Canvas canvas) {        if (measuredHeight <= 0 || measureDWIDth <= 0 || mNumberOfTabs == 0) {            return; // Not much we can draw :(        }        int length = measureDWIDth / mNumberOfTabs;        int startX = (currentTab - 1) * length;        canvas.drawRect(startX, 0, startX + length, measuredHeight, mIndicatorPaint);    }}

接下来是ZoomOutPagetransformer.这个人是Google借来的.

import androID.support.v4.vIEw.VIEwPager;import androID.vIEw.VIEw;public class ZoomOutPagetransformer implements VIEwPager.Pagetransformer {    private static final float MIN_SCALE = 0.85f;    private static final float MIN_Alpha = 0.5f;    public voID transformPage(VIEw vIEw, float position) {        int pageWIDth = vIEw.getWIDth();        int pageHeight = vIEw.getHeight();        if (position < -1) { // [-Infinity,-1)            // This page is way off-screen to the left.            vIEw.setAlpha(0);        }        else if (position <= 1) { // [-1,1]            // Modify the default slIDe Transition to shrink the page as well            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));            float vertmargin = pageHeight * (1 - scaleFactor) / 2;            float horzmargin = pageWIDth * (1 - scaleFactor) / 2;            if (position < 0) {                vIEw.setTranslationX(horzmargin - vertmargin / 2);            }            else {                vIEw.setTranslationX(-horzmargin + vertmargin / 2);            }            // Scale the page down (between MIN_SCALE and 1)            vIEw.setScaleX(scaleFactor);            vIEw.setScaleY(scaleFactor);            // Fade the page relative to its size.            vIEw.setAlpha(MIN_Alpha + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_Alpha));        }        else { // (1,+Infinity]            // This page is way off-screen to the right.            vIEw.setAlpha(0);        }    }}

接下来制作一个名为dummy_fragment_layout.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:ID="@+ID/fragmentNumber"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:layout_centerVertical="true"        androID:gravity="center_horizontal"        androID:text="1"        androID:textSize="50sp"        androID:textStyle="bold"/>    <TextVIEw        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:layout_above="@ID/fragmentNumber"        androID:gravity="center_horizontal"        androID:text="Fragment"        androID:textSize="30sp"/></relativeLayout>

现在主要活动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"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:background="#808080"    androID:orIEntation="vertical"    >    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="?actionbarSize"        androID:background="#26292E"        androID:orIEntation="horizontal">        <ImageVIEw            androID:ID="@+ID/tab1"            androID:layout_wIDth="0dip"            androID:layout_height="wrap_content"            androID:layout_weight="1"            androID:adjustVIEwBounds="true"            androID:onClick="onTabSelected"            androID:padding="10dp"            androID:src="@androID:drawable/btn_radio"            androID:tag="1"/>        <ImageVIEw            androID:ID="@+ID/tab2"            androID:layout_wIDth="0dip"            androID:layout_height="wrap_content"            androID:layout_weight="1"            androID:adjustVIEwBounds="true"            androID:onClick="onTabSelected"            androID:padding="10dp"            androID:src="@androID:drawable/btn_star"            androID:tag="2"/>        <ImageVIEw            androID:ID="@+ID/tab3"            androID:layout_wIDth="0dip"            androID:layout_height="wrap_content"            androID:layout_weight="1"            androID:adjustVIEwBounds="true"            androID:onClick="onTabSelected"            androID:padding="10dp"            androID:src="@androID:drawable/btn_star"            androID:tag="3"/>        <ImageVIEw            androID:ID="@+ID/tab4"            androID:layout_wIDth="0dip"            androID:layout_height="wrap_content"            androID:layout_weight="1"            androID:adjustVIEwBounds="true"            androID:onClick="onTabSelected"            androID:padding="10dp"            androID:src="@androID:drawable/btn_star"            androID:tag="4"/>        <ImageVIEw            androID:ID="@+ID/tab5"            androID:layout_wIDth="0dip"            androID:layout_height="wrap_content"            androID:layout_weight="1"            androID:adjustVIEwBounds="true"            androID:onClick="onTabSelected"            androID:padding="10dp"            androID:src="@androID:drawable/btn_star"            androID:tag="5"/>    </linearLayout>    <test.kseneman.si.test.SimpleTabIndicator        androID:ID="@+ID/tabIndicator"        androID:layout_wIDth="match_parent"        androID:layout_height="2dp"        androID:background="#26292E"        app:indicatorcolor="#FFFDE992"        app:numberOfTabs="5"/>    <androID.support.v4.vIEw.VIEwPager        androID:ID="@+ID/pager"        xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"/></linearLayout>

最后是MainActivity

import androID.os.Bundle;import androID.support.v4.app.Fragment;import androID.support.v4.app.FragmentManager;import androID.support.v4.app.FragmentStatePagerAdapter;import androID.support.v4.vIEw.VIEwPager;import androID.support.v7.app.ActionBaractivity;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.ImageVIEw;public class MainActivity extends ActionBaractivity {    private VIEwPager mPager;    private DummyFragmentsAdapter mPagerAdapter;    private SimpleTabIndicator tabIndicator;    private ImageVIEw selectedImageVIEw;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        mPagerAdapter = new DummyFragmentsAdapter(getSupportFragmentManager());        tabIndicator = (SimpleTabIndicator) findVIEwByID(R.ID.tabIndicator);        // Default state        selectedImageVIEw = (ImageVIEw) findVIEwByID(R.ID.tab1);        mPager = (VIEwPager) findVIEwByID(R.ID.pager);        mPager.setAdapter(mPagerAdapter);        mPager.setPagetransformer(false, new ZoomOutPagetransformer());    }    public voID onTabSelected(VIEw v) {        // Sanity check        if (v == null || !(v instanceof ImageVIEw) || v.getTag() == null) {            return;        }        int postion = Integer.valueOf((String) v.getTag());        Log.d("onTabSelected", "postion: " + postion);        if (postion == mPager.getCurrentItem() + 1) {            // The same selected, do nothing?            return;        }        // Change selected images        selectedImageVIEw.setimageResource(androID.R.drawable.btn_star);        selectedImageVIEw = (ImageVIEw) v;        selectedImageVIEw.setimageResource(androID.R.drawable.btn_radio);        mPager.setCurrentItem(postion - 1); // They start at 0        tabIndicator.setCurrentTab(postion);    }    private class DummyFragmentsAdapter extends FragmentStatePagerAdapter {        public DummyFragmentsAdapter(FragmentManager fm) {            super(fm);        }        @OverrIDe        public Fragment getItem(int position) {            DummyFragment fragment = new DummyFragment();            Bundle b = new Bundle();            b.putInt(DummyFragment.EXTRA_FRAGMENT_NUMBER, position);            fragment.setArguments(b);            return fragment;        }        @OverrIDe        public int getCount() {            return 5;        }    }}

完整的项目zip可用here

这是它的样子

总结

以上是内存溢出为你收集整理的Android:不推荐使用非滑动式标签页?全部内容,希望文章能够帮你解决Android:不推荐使用非滑动式标签页?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078661.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存