Android - 我可以增加 NumberPicker 小部件的文本大小吗?

发布于 2024-11-27 21:40:31 字数 116 浏览 1 评论 0 原文

我们在 3.0 中得到了一个 NumberPicker 小部件,但似乎该小部件的 textSize 无法修改。我错过了什么还是事实如此?我真的很想增加字体大小,它的默认值相当小。但我看不到它的 textSize 属性。

We got a NumberPicker widget in 3.0, but it seems that the textSize for this widget can't be modified. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

秋风の叶未落 2024-12-04 21:40:32

我面临着同样的问题,我想增加 NumberPicker 的文本大小,但找不到将其作为小部件的一部分来实现的方法。上面的所有答案都很棒,但我采用了以下解决方案,它满足了我的需求:

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

这本质上放大了整个小部件。

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. 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 magnifies the entire Widget.

无声无音无过去 2024-12-04 21:40:32

以下内容对我有用(在 API 15 / 4.0.3 中)

注意: aheuermann 的解决方案可以安全地防止不同 Android 版本之间 NumberPicker 布局中的潜在实现差异(即也就是说,TextView 可能并不总是位于子位置 1)。

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

The following worked for me (in 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).

TextView tv1 = (TextView)_numberPicker.getChildAt(1);
tv1.TextSize = 10;
叶落知秋 2024-12-04 21:40:32

跳到API 23,aheuermann提出的解决方案似乎有问题。在 NumberPicker 的源代码中,文本大小是根据初始化时的 EditText 子级设置的。如果您稍后尝试更改它,EditText 字段将会变大,但由小部件直接绘制的所有其他数字仍然会很小。

我要使用的解决方案的缺点是它必须以编程方式实例化,而不是通过布局实例化,但除此之外,它似乎按预期工作。首先,将以下内容添加到您的 styles.xml 文件中:

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

然后,您可以实例化 NumberPicker,

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

假设它是在 Activity 中完成的。如果没有,请将“this”替换为您可用的任何上下文。此代码相当于设置一个覆盖 Activity 中所有 TextView 大小的主题,但仅将其应用于 NumberPicker 及其子项。

Jumping to API 23, the solution presented by aheuermann seems to have problems. In the source for NumberPicker, the text size is set based on the EditText child at initialization. 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.

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:

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

Then, you can instantiate the NumberPicker with

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

assuming that it's being done within an Activity. If not, replace the "this" with whatever Context you have available. 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.

无法言说的痛 2024-12-04 21:40:32

我们遇到了同样的问题,最终重新创建了 NumberPicker 本地。 这里有一个更深入的教程

We ran into the same problem and ended up recreating NumberPicker locally. There's a more in depth tutorial here.

2024-12-04 21:40:32

我们可以在 NumberPicker 中自定义视图。

它具有三个视图-

2 个图像按钮和 1 个编辑文本。

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

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>

We can customize views inside NumberPicker.

It has three views-

2 ImageButttons and 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

<?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>
万劫不复 2024-12-04 21:40:32

从 API 29 开始,您可以使用方法:

number_picker.setTextSize( float textSize );

From API 29 you can use method:

number_picker.setTextSize( float textSize );
゛时过境迁 2024-12-04 21:40:31

我可以通过扩展默认的 NumberPicker 来完成此任务。不理想,但它有效。

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"));
     }
   }

 }

然后只需在布局 xml 中引用此类即可。

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

I was able to accomplish this by extending the default 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.

<com.yourpackage.NumberPicker
        android:id="@+id/number_picker"
        android:layout_width="43dp"
        android:layout_height="wrap_content" />
夏天碎花小短裙 2024-12-04 21:40:31

仅为您的 NumberPicker 设置此样式:

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

@android:color/white
更改文本颜色:
输入图像描述这里

only set this sytle for your NumberPicker:

   <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 :
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文