【问题标题】:How to center tab text vertically如何使标签文本垂直居中
【发布时间】:2015-04-28 16:00:59
【问题描述】:

我为我的 Android 应用中的选项卡活动自定义了选项卡布局。问题是如何垂直居中。文本自动水平居中。我试着写gravity:centerlayout_gravity:center。更重要的是:我尝试在我的TextView 中写出精确的高度,但没有任何效果(实际上精确的像素效果很好,但是当我改变设备方向时 - 文本不是中心方向的。

这是我的标签布局代码:

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

    <TextView
        android:id="@+id/tab_name"
        android:paddingTop="11dp"
        android:paddingBottom="11dp"
        android:layout_width="match_parent"
        android:textSize="14dp"
        android:textStyle="bold"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:maxLines="1"
        android:layout_gravity="center" />

</LinearLayout>

这是我的问题的屏幕截图:

【问题讨论】:

  • 我不太确定出了什么问题。你能发布一些可以帮助的图像。另外 dp 中 textview 的高度和宽度是多少?
  • 你想让文本或小部件垂直居中什么?
  • @ngoa 我用我的问题的图片链接更新了帖子
  • @StepanMazokha 您是否尝试使用 actionBar 并看到它有效?只是一个建议!
  • @ngoa 你说“使用actionBar”是什么意思?顺便说一句,我以这种方式以编程方式添加选项卡: mCustomView = (LinearLayout) getLayoutInflater().inflate(R.layout.custom_tab, null); textView = (TextView)mCustomView.findViewById(R.id.tab_name); textView.setText(mSectionsPagerAdapter.getPageTitle(i)); actionBar.addTab(actionBar.newTab().setCustomView(mCustomView).setTabListener(mActivity));

标签: android tabs android-viewpager android-tabhost


【解决方案1】:

您只需将android:gravity="center" 添加到LinearLayout
wrap_content 用于layout_widthlayout_heightTextView

<?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:gravity="center">

    <TextView
        android:id="@+id/tab_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="11dp"
        android:paddingBottom="11dp"
        android:textSize="14dp"
        android:textStyle="bold"
        android:maxLines="1" />

</LinearLayout>

【讨论】:

  • 不幸的是,这对我不起作用。我更新了我的帖子,并附上了我的问题的截图链接(代码,你在你的答案中给出的代码被应用在那里)
  • 请在开发者选项中启用Show layout bounds 并发布额外的截图:)
  • 我在帖子中添加了截图
  • 看起来有一些padding。也许这个答案有帮助:stackoverflow.com/a/23241518/4193263
  • 感谢您的帮助,但帮助我的东西写在这里:stackoverflow.com/a/22144844/3649567
猜你喜欢
  • 2016-07-29
  • 1970-01-01
  • 2020-08-12
  • 1970-01-01
  • 1970-01-01
  • 2013-10-15
  • 2012-12-28
  • 1970-01-01
  • 2022-11-15
相关资源
最近更新 更多