【问题标题】:Android TabLayout height does not increase with font sizeAndroid TabLayout高度不随字体大小增加
【发布时间】:2017-07-13 22:26:09
【问题描述】:

以下是我的TabLayout代码

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    app:tabIndicatorHeight="0dp"
    app:tabSelectedTextColor="@android:color/white"
    app:tabTextColor="@color/city_grey"
    app:tabTextAppearance="@style/DashboardTabLayoutTextAppearance"
    android:paddingTop="@dimen/dp10"
    android:paddingBottom="@dimen/dp10"
    app:tabPaddingStart="@dimen/dp20"
    app:tabPaddingEnd="@dimen/dp20"
    app:tabMode="scrollable"
    app:tabGravity="fill"/>

而 DashboardTabLayoutTextAppearance 是

<style name="DashboardTabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textSize">@dimen/sp40</item>
    <item name="android:fontFamily">sans-serif-light</item>
</style>

可以看出TabLayout的高度已设置为wrap_content,标签文本textSize已发送至40sp

所以TabLayout 的高度自然会随着文本高度的增加而增加。但在某些设备上,它会垂直剪切文本。

这是 7 英寸标签上的结果

而在 5.5 英寸的手机上它工作得非常好

我尝试使用 layout_marginpadding 属性,但没有解决问题。

编辑 1: 添加代码以显示我正在初始化 TabLayoutViewPager

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
    adapter.addFragment(DashboardWeeklyFragment.newInstance(weeklyData), "Weekly");
    adapter.addFragment(DashboardMonthlyFragment.newInstance(monthlyData), "Monthly");
    adapter.addFragment(DashboardDailyFragment.newInstance(dailyData), "Daily");
    viewPager.setAdapter(adapter);
}

【问题讨论】:

  • 您需要提供动态高度,因为 tablayout 有自己的尺寸,如工具栏,尝试使用 dimen xml 或以编程方式。希望能成功!!!
  • 为您提供固定高度 TabLayout 可能会解决您的问题
  • 同意以编程方式将高度设置为整个屏幕高度的某个百分比
  • 但是我如何知道需要根据fontSize 设置的高度?因为在每个屏幕上都会有所不同。
  • 如果使用 wrap_content 那么它会自动处理。发布您的 xml 文件以了解更多信息。此外,您如何设置文本视图也很重要

标签: android android-tablayout


【解决方案1】:

int 高度 = 50;

让它像....一样动态 tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = (int) (height * this.getResources().getDisplayMetrics().density);

【讨论】:

  • 首先,我使用的是TabLayout而不是TabHost,其次,我不能确定50在所有情况下都是完美的高度。
猜你喜欢
  • 2011-12-03
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多