簡體   English   中英

Android - 我可以為NumberPicker小部件增加textSize嗎?

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

我們在3.0中獲得了一個NumberPicker小部件,但似乎無法修改此小部件的textSize。 我錯過了什么或是這樣嗎? 我真的想增加字體大小,它的默認值非常小。 但是我看不到它的textSize屬性。

我能夠通過擴展默認的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" />

我遇到了同樣的問題,我想增加NumberPicker的文本大小,但無法找到一種方法來作為Widget的一部分。 以上所有答案都很棒,但我選擇了滿足我需求的以下解決方案:

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

這基本上放大了整個Widget。

只為你的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" />

<item name="android:textColorPrimary">@android:color/white</item>更改textcolor: 在此輸入圖像描述

以下內容適用於我(在API 15 / 4.0.3中)

注意:aheuermann的解決方案是針對潛在的實現差異安全NumberPicker的在不同的Android版本的布局(即, TextView可能並不總是在孩子1位)。

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

我們遇到了同樣的問題,最終在本地重新創建了NumberPicker 有一個更深入的教程在這里

跳到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及其子代。

我們可以在NumberPicker中自定義視圖。
它有三個觀點 -
2個ImageButttons和1個EditText。

    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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM