【问题标题】:how to set floating label text size?如何设置浮动标签文字大小?
【发布时间】:2016-02-15 11:33:08
【问题描述】:

我想在Android材质EditText中更改浮动标签文字大小,当我设置如下:

<android.support.v7.widget.AppCompatEditText
    android:id="@+id/edt_current"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/str_current"
    android:inputType="number"
    android:singleLine="true"
    android:textSize="20sp" />

它只是改变提示和输入文本的文本大小。

由于浮动标签文本大小在我的 UI 中似乎太小,我想更改它,有什么解决方案吗?

【问题讨论】:

    标签: android android-edittext material-design


    【解决方案1】:

    试试下面的代码。这可能会对您有所帮助:

    <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:hintTextAppearance="@style/TextAppearance.AppCompat.Medium.Inverse">
    

    您可以改用app:hintTextAppearance="@style/TextAppearance.AppCompat.Small.Inverse"

    【讨论】:

    • 非常感谢 :) 你真的拯救了我的一天 :) 我不知何故使用了你的答案来达到我的目标,但你的答案让真正的方式变得轻松:) 我会在下面为其他人添加我的答案:)
    【解决方案2】:

    接受的答案对我找到解决方案有很大帮助(即设置确切的字体大小,而不仅仅是使用小号、中号……)。

    另外,通过定义app:hintTextAppearance的样式,也可以简单的设置浮动标签的颜色:)

    在styles.xml中:

    <style name="CustomTextAppearance" parent="@android:style/TextAppearance">
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">@color/colorAccent</item>
    </style>
    

    在您的 TextInputLayout 中:

    <android.support.design.widget.TextInputLayout
    android:id="@+id/lyt_goal"
    style="@style/CustomTextInput"
    app:hintTextAppearance="@style/CustomTextAppearance">
    
    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/edt_goal"
        android:hint="@string/str_goal" />
    

    【讨论】:

    • 大小和颜色可以正常工作,但如果我尝试更改文本样式...没有任何改变。
    • @BajrangHudda 你想以什么方式改变风格?给我一个例子。因为尺寸和颜色也是风格的一部分:-)
    • @par4301bold 怎么样?这不起作用
    • style=@style/CustomEditText 是解决方案的一部分吗?如果是:请发布样式。如果不是:删除它以提供更清晰的答案。
    【解决方案3】:

    试试下面的代码,它在正常状态下工作

     <android.support.design.widget.TextInputLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:theme="@style/TextLabel">
    
         <android.support.v7.widget.AppCompatEditText
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:hint="Hiiiii"
             android:id="@+id/edit_id"/>
    
     </android.support.design.widget.TextInputLayout>
    

    在样式文件夹中的 TextLabel 代码

     <style name="TextLabel" parent="TextAppearance.AppCompat">
        <!-- Hint color and label color in FALSE state -->
        <item name="android:textColorHint">@color/Color Name</item> 
        <item name="android:textSize">20sp</item>
        <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
        <item name="colorAccent">@color/Color Name</item>
        <item name="colorControlNormal">@color/Color Name</item>
        <item name="colorControlActivated">@color/Color Name</item>
     </style>
    

    设置为应用程序的主题,它仅适用于仅突出显示状态

     <item name="colorAccent">@color/Color Name</item>
    

    希望对你有帮助!

    【讨论】:

    • 仔细阅读OP的问题,他/她需要设置浮动标签大小
    • TextInputLayout 用于浮动标签 @NJ 。这就是我提出这个解决方案的原因。希望对您有所帮助!
    • 谢谢拉胡尔,我在问问题之前使用了它,但不幸的是,与我在主要问题中问的一样,它只是改变提示文本和输入文本的大小,我想改变浮动标签文本大小,当它变小并移动到editText的顶部时。
    • 如果我只希望样式用于提示而不是 Edittext 中的真实文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2021-11-30
    • 2017-02-24
    相关资源
    最近更新 更多