【问题标题】:Cannot vertically align 2 TextViews within a LinearLayout无法在 LinearLayout 中垂直对齐 2 个 TextView
【发布时间】:2018-08-10 20:31:12
【问题描述】:

我有一张日历图片 (72x72)。我正在尝试将月份叠加在顶部,将日期叠加在主要位上。

我试图通过使用垂直 LinearLayout 来做到这一点,日历设置为背景图像,两个 TextViews 作为 LinearLayout 的子级。

我已经让它工作到一定程度,但我无法让月份充分下降以使其处于正确的位置。

这张图片是我目前所取得的成果:

使用以下布局片段:

 <LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/cal"
    android:layout_gravity="center"
    android:weightSum="8">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="September"
        android:id="@+id/textView8"
        android:layout_weight="5"
        android:gravity="center_horizontal|bottom"
        android:textColor="#ffffff"
        android:textSize="10dp"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:paddingBottom="-10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Display1"
        android:text="21"
        android:id="@+id/textView9"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_weight="3"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="0dp"
        android:paddingTop="0dp" />

</LinearLayout>

我一直在玩重量和填充(如您所见),但我尝试过的没有任何效果。

谁能建议我应该怎么做? (如果可能的话,我不希望更改文本大小。)

答案是按照 Hussein El Feky(和 Brendan Shaenzer)的建议在日期月份 TextView 的顶部添加填充。所以谢谢你们。

这是修正后的图像:

【问题讨论】:

  • 我试图了解当您实际赋予它 3/8 权重时,TextView 是如何占用这么多空间的。是因为风格吗?

标签: android android-layout android-linearlayout textview android-layout-weight


【解决方案1】:

试试这个布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/cal">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="September"
        android:id="@+id/textView8"
        android:layout_weight="5"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="10dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Display1"
        android:text="21"
        android:id="@+id/textView9"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_weight="7"
        android:gravity="center" />

</LinearLayout>

如果您可以提供您的 cal drawable,我也将能够改进我的答案。我让你的 TextViews 居中,但现在玩权重,因为我没有你的 drawable。

【讨论】:

  • 嗨,不知道如何在 cmets 中添加图像,但它是 72x72,红/白边框为 30,因此权重为 1 到 1.4。我认为这是填充/边距/ll 分隔符的问题,我需要做更多的调查。我希望可能有一个简单的解决方案:(
  • 如果您的红色部分从 0 到 30 (30) 开始,而白色部分从 30 到 72 (42) 开始,那么布局权重应该看起来像 30:42 简化为 5:7。我已经更新了我的答案。您现在唯一应该做的就是将 paddingTop 添加到您的第一个 TextView(月)并将其调整为顶部的两个白色矩形。如果您需要更多帮助,请告诉我。
  • 是的,添加 paddingtop 解决了它。它稍微扩展了背景图像,但我可以通过调整日期文本大小将其调整为可接受的值。非常感谢,我已将更正后的图像添加到原始问题中。
【解决方案2】:

看起来您需要在第一个 TextViewLinearLayout 的顶部之间留出空间,但我没有看到任何可以实现这一点的属性。任何一个:

  • android:paddingTop textView8
  • android:layout_marginToptextView8
  • android:paddingTop LinearLayout

应该这样做。

【讨论】:

  • 感谢您的建议,我认为您是对的,但我需要做更多的实验。正如我在之前的评论中所说,我希望可能有一些简单的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
  • 2011-05-13
相关资源
最近更新 更多