简体   繁体   English

Android - 我可以为NumberPicker小部件增加textSize吗?

[英]Android - can I increase the textSize for the NumberPicker widget?

We got a NumberPicker widget in 3.0, but it seems that the textSize for this widget can't be modified. 我们在3.0中获得了一个NumberPicker小部件,但似乎无法修改此小部件的textSize。 Am I missing something or is this the case? 我错过了什么或是这样吗? I'd really like to increase the font size, it's quite small with the default value. 我真的想增加字体大小,它的默认值非常小。 But I can't see a textSize property for it. 但是我看不到它的textSize属性。

I was able to accomplish this by extending the default NumberPicker. 我能够通过扩展默认的NumberPicker来实现这一目标。 Not ideal, but it works. 不理想,但它的工作原理。

public class NumberPicker extends android.widget.NumberPicker {

   public NumberPicker(Context context, AttributeSet attrs) {
     super(context, attrs);
   }

   @Override
   public void addView(View child) {
     super.addView(child);
     updateView(child);
   }

   @Override
   public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
     super.addView(child, index, params);
     updateView(child);
   }

   @Override
   public void addView(View child, android.view.ViewGroup.LayoutParams params) {
     super.addView(child, params);
     updateView(child);
   }

   private void updateView(View view) {
     if(view instanceof EditText){
       ((EditText) view).setTextSize(25);
       ((EditText) view).setTextColor(Color.parseColor("#333333"));
     }
   }

 }

Then just reference this class in your layout xml. 然后在布局xml中引用此类。

<com.yourpackage.NumberPicker
        android:id="@+id/number_picker"
        android:layout_width="43dp"
        android:layout_height="wrap_content" />

I was facing the same problem, I wanted to increase the text size of the NumberPicker but couldn't find a way to do it as part of the Widget. 我遇到了同样的问题,我想增加NumberPicker的文本大小,但无法找到一种方法来作为Widget的一部分。 All the above answers are great, but instead I went with the following solution, which met my needs: 以上所有答案都很棒,但我选择了满足我需求的以下解决方案:

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleX="1.5"
    android:scaleY="1.5"/>

This essentially magnifying the entire Widget. 这基本上放大了整个Widget。

only set this sytle for your NumberPicker: 只为你的NumberPicker设置这个sytle:

   <style name="AppTheme.Picker" parent="Theme.AppCompat.Light.NoActionBar" >
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textSize">20dp</item>
</style>

<NumberPicker
        android:theme="@style/AppTheme.Picker"
        android:id="@+id/yearNumberPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

and <item name="android:textColorPrimary">@android:color/white</item> change textcolor : <item name="android:textColorPrimary">@android:color/white</item>更改textcolor: 在此输入图像描述

The following worked for me (in API 15 / 4.0.3) 以下内容适用于我(在API 15 / 4.0.3中)

note: aheuermann's solution is safe against potential implementation differences in the NumberPicker 's layout across different Android versions (that is, the TextView may not always be at child position 1). 注意:aheuermann的解决方案是针对潜在的实现差异安全NumberPicker的在不同的Android版本的布局(即, TextView可能并不总是在孩子1位)。

TextView tv1 = (TextView)_numberPicker.getChildAt(1);
tv1.TextSize = 10;

We ran into the same problem and ended up recreating NumberPicker locally. 我们遇到了同样的问题,最终在本地重新创建了NumberPicker There's a more in depth tutorial here . 有一个更深入的教程在这里

Jumping to API 23, the solution presented by aheuermann seems to have problems. 跳到API 23,aheuermann提出的解决方案似乎有问题。 In the source for NumberPicker, the text size is set based on the EditText child at initialization. 在NumberPicker的源中,文本大小是在初始化时根据EditText子项设置的。 If you try to change it later, the EditText field will get big, but all other numbers drawn directly by the widget will still be small. 如果您稍后尝试更改它,EditText字段将变大,但窗口小部件直接绘制的所有其他数字仍然很小。

The solution I'm going with has the disadvantage that it must be instantiated programmatically, instead of via layout, but otherwise, it seems to work as intended. 我要使用的解决方案的缺点是它必须以编程方式实例化,而不是通过布局,但除此之外,它似乎按预期工作。 First, add the following to your styles.xml file: 首先,将以下内容添加到styles.xml文件中:

<style name="NumberPickerText">
    <item name="android:textSize">40dp</item>
</style>

Then, you can instantiate the NumberPicker with 然后,您可以使用实例化NumberPicker

    ContextThemeWrapper cw = new ContextThemeWrapper(this, R.style.NumberPickerText);
    NumberPicker np = new NumberPicker(cw);

assuming that it's being done within an Activity. 假设它是在一个Activity中完成的。 If not, replace the "this" with whatever Context you have available. 如果没有,请将“this”替换为您可用的任何上下文。 This code does the equivalent of setting a theme that overrides all TextView sizes within your Activity, but only applies it to the NumberPicker and its children. 此代码相当于设置一个主题,该主题覆盖Activity中的所有TextView大小,但仅将其应用于NumberPicker及其子代。

We can customize views inside NumberPicker. 我们可以在NumberPicker中自定义视图。
It has three views- 它有三个观点 -
2 ImageButttons and 1 EditText. 2个ImageButttons和1个EditText。

    if(numberPicker.getChildAt(1) instanceOf EditText)
    {
        EditText edt=(EditText)numberPicker.getChildAt(1);
        //do customizations here
    } 

Complete layout of NumberPicker from Android Source Code 从Android源代码完整布局NumberPicker

<?xml version="1.0" encoding="utf-8"?>
<!--
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageButton android:id="@+id/increment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/numberpicker_up_btn"
        android:paddingTop="22dip"
        android:paddingBottom="22dip"
        android:contentDescription="@string/number_picker_increment_button" />

    <EditText
        android:id="@+id/numberpicker_input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.Large.Inverse.NumberPickerInputText"
        android:gravity="center"
        android:singleLine="true"
        android:background="@drawable/numberpicker_input" />

    <ImageButton android:id="@+id/decrement"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/numberpicker_down_btn"
        android:paddingTop="22dip"
        android:paddingBottom="22dip"
        android:contentDescription="@string/number_picker_decrement_button" />

</merge>

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

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