简体   繁体   English

使用TextView在顶部和底部进行Android不需要的填充

[英]Android unwanted padding on top and bottom with TextView

I am trying to put two text view within the circular progress bar as shown in the picture below. 我试图在圆形进度条中放置两个文本视图,如下图所示。 These two text should be aligned with center of the circular progress bar. 这两个文本应与圆形进度条的中心对齐。 在此输入图像描述

But the unwanted padding in text view which prevents me to align the text into the center of the circular progress bar. 但文本视图中不需要的填充阻止我将文本对齐到圆形进度条的中心。 This is what i get. 这就是我得到的。

在此输入图像描述 在此输入图像描述

here is my layout file: 这是我的布局文件:

 <FrameLayout
            android:id="@+id/onboarding_activity_progress_layout"
            android:layout_gravity="center_horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp">

            <com.locationlabs.finder.android.core.ui.CircularProgressBar
                  android:id="@+id/onboarding_activity_progress_bar"
                  android:layout_gravity="center"
                  android:padding="10dp"
                  android:layout_width="120dp"
                  android:layout_height="120dp"
                  app:progressColor="@color/progress_bar_color"
                  app:backgroundColor="@color/progress_bar_background_color"
                  app:progressBarWidth="10dp"
                  tools:progress="60"/>
            <LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:includeFontPadding="false"
                  android:layout_gravity="center">

                <TextView
                      android:id="@+id/number_of_remaining_steps"
                      style="@style/OnboardingNumberOfRemainingStepText"
                      tools:text="2"/>

                <TextView
                      android:id="@+id/remaining_number_of_steps"
                      style="@style/OnboardingRemainingStepText"
                      tools:text="steps left"/>

            </LinearLayout>
        </FrameLayout>

Here is the styling: 这是造型:

<style name="OnboardingNumberOfRemainingStepText">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_gravity">center_horizontal</item>
        <item name="android:fontFamily">sans-serif</item>
        <item name="android:textSize">40sp</item>
        <item name="android:textColor">@color/dark_grey</item>
        <item name="android:includeFontPadding">false</item>
    </style>

  <style name="OnboardingRemainingStepText">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_gravity">center_horizontal</item>
        <item name="android:fontFamily">sans-serif-light</item>
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">@color/dark_grey</item>
        <item name="android:textStyle">normal</item>
    </style>

I was told android:includeFontPadding can help reduce the padding but it doesn't help that much. 我被告知android:includeFontPadding可以帮助减少填充,但它没有那么多帮助。 I also tried with the single textView with Spannable but I get almost the similar result. 我也尝试使用带有Spannable的单个textView,但我得到了几乎相似的结果。 I tried with negative margin but that didn't work and it is also not reliable. 我尝试使用负边距,但这不起作用,也不可靠。

When you transfer styling attributes of a TextView to XML, TextView might render in an unexpected way. 将TextView的样式属性传输到XML时,TextView可能会以意外方式呈现。 This is because the default styling for TextView is applied in a layout file which is overridden when you use style attribute in it. 这是因为TextView的默认样式应用于布局文件中,当您在其中使用样式属性时会覆盖该布局文件。

You can try using parent="android:TextAppearance" in the style defined for a TextView to ensure you inherit the pre-defined styles for the TextView. 您可以尝试在为TextView定义的样式中使用parent =“android:TextAppearance” ,以确保继承TextView的预定义样式。

Example: 例:

Let me know if it worked for you! 让我知道它是否适合你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM