gpt4 book ai didi

android - TabLayout 如何设置固定计数?

转载 作者:行者123 更新时间:2023-11-29 15:44:27 25 4
gpt4 key购买 nike

我将 TabLayout 与 viewPager 一起使用,而 viewPager 将 RecyclerView 用于扩展 FragmentStatePageAdapter 的 fragment 。

一切正常。但是,我有一个场景,我想将可滚动模式下的 tabLayout 的项目数设置为仅 (7)(当 tabMode 设置为可滚动时)。

不知何故,当 tabLayout 设置为可滚动时,它只为每个设备设置 5 个项目,但在我的场景中,我希望它调整 7 个项目,而不管任何设备。

我尝试将项目宽度设置为 totalDeviceWidht/7 ,但没有成功,因为它仍然采用相同的宽度。

我还尝试通过编写自定义 tabLayout 来覆盖 onMeasure() 方法。但是,运气不好。

我不确定问题是否与 pagerTabStrip 有关,或者当 tabMode 设置为可滚动时,viewPager 和 tabLayout 如何计算要修复的项目数。

过去 3 天我一直被这个问题困扰。

这是我的主要 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"

app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.movie.bms.views.activities.TimeActivity"
tools:showIn="@layout/activity_time"
>

<android.support.design.widget.TabLayout
android:id="@+id/time_sliding_tab"
android:layout_width="match_parent"
android:layout_height="61.8dp"
android:background="#06acef"
android:elevation="0dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:elevation="@dimen/elevation.small"
app:tabGravity="center"
app:tabIndicatorColor="@color/white"
app:tabMode="scrollable"
app:tabPaddingEnd="-1dp"
app:tabPaddingStart="-1dp"
android:fillViewport="false"
></android.support.design.widget.TabLayout>

<com.utils.customcomponents.CustomViewPager
android:id="@+id/time_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transitionName="@string/event_body_transition">
</com.utils.customcomponents.CustomViewPager>
</LinearLayout>

如有任何帮助或建议,我们将不胜感激。

最佳答案

您可以通过在 xml 中的 TabLayout 上设置 app:tabMinWidthapp:tabMaxWidth 元素来影响选项卡的实际宽度。

例如,对于 Nexus 5 (360dp):

<android.support.design.widget.TabLayout
android:id="@+id/time_sliding_tab"
android:layout_width="match_parent"
android:layout_height="62dp"
android:background="#06acef"
app:tabGravity="center"
app:tabMaxWidth="50dp"
app:tabMinWidth="50dp"
app:tabMode="scrollable" />

但说实话,您需要估计“猜测”您的目标宽度并为此进行优化。它在实际设备上的外观取决于其实际宽度。

如果你想让它 100% 适合你需要:

  • 在代码中实例化 TabLayout 并传入正确的 app:tabMinWidthtabMaxWidth 值作为 AttributeSet . (你根据屏幕宽度计算的)
  • 使用reflection在现有的 TabLayout 实例上设置 mRequestedTabMinWidthmRequestedTabMaxWidth 私有(private)字段。 (不推荐)

反射案例如何工作的示例:

import android.content.Context;
import android.support.design.widget.TabLayout;
import android.util.AttributeSet;

import java.lang.reflect.Field;

public class SevenTabLayout extends TabLayout {

public class SetTabWidthException extends RuntimeException {

}

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

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

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

public void initializeTabWidths() {
int tabwidth = 140; // Screensize divided by 7
setPrivateField("mRequestedTabMinWidth", tabwidth);
setPrivateField("mRequestedTabMaxWidth", tabwidth);
}

private void setPrivateField(String tabMinWidthFieldName, int value) {
try {
Field f = TabLayout.class.getDeclaredField(tabMinWidthFieldName);
f.setAccessible(true);
f.setInt(this, value);
} catch (NoSuchFieldException e) {
throw new SetTabWidthException();
} catch (IllegalAccessException e2) {
throw new SetTabWidthException();
}
}
}

现在您可以轻松地为 initializeTabWidths() 方法添加单元测试,这样您就可以在升级设计支持库后检测该方法何时停止工作。

关于android - TabLayout 如何设置固定计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35069237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com