当前位置: 首页 > 知识库问答 >
问题:

如何通过编程为Android Spinner项目设置文本大小?

曾昂然
2023-03-14

我的情况似乎与谷歌上发现的100万个问题(与spinner相关)有点不同,google上的主要解决方案只是为spinner项目创建自定义xml

Spinner mySpinner = FindViewById<Spinner>(Resource.Id.mySpinner);
List<string> languages = new List<string>();
Translation.LoadAvailableLanguages(out languages);
var adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, languages);
adapter.SetDropDownViewResource(Resource.Layout.spinner_item);
mySpinner.Adapter = adapter;
adapter.GetDropDownView(int position, View convertView, ViewGroup parent);

位置int:我们想要其视图的项的索引。

convertView视图:如果可能的话,要重用的旧视图。注意:在使用之前,您应该检查此视图是否为非空视图并具有适当的类型。如果无法转换此视图以显示正确的数据,此方法可以创建新视图。

父ViewGroup:此视图最终将附加到的父视图

(mySpinner.GetChildAt(n) as TextView).SetTextSize(Android.Util.ComplexUnitType.Px, 100);

Edit2:解决方案
基于公认的答案,我已经将Java for android代码转换为C#/Xamarin for android

class SpinnerStringAdapter: Android.Widget.ArrayAdapter {
    /// <summary>
    /// Array items' text height in screen pixels
    /// </summary>
    private int ActualTextSize = 20;
    /// <summary>
    /// Display items as bold text
    /// </summary>
    private bool IsBold = false;
    /// <summary>
    /// Creates array adapter with custom sized text.
    /// </summary>
    /// <param name="AContext">The current context.</param>
    /// <param name="ALayoutID">The resource ID for a layout file containing a TextView to use when instantiating views.</param>
    /// <param name="AStringList">The string objects to represent in the ListView.</param>
    /// <param name="ADisplay">The display of the parent activity</param>
    /// <param name="ATextSize">The size of a text for the array items. Size in percents of Display height</param>
    public SpinnerStringAdapter(Context AContext, int ALayoutID, System.Collections.IList AStringList, Display ADisplay, double ATextSize, bool AIsBold):
    base(AContext, ALayoutID, AStringList) {            
        Android.Graphics.Point size = new Android.Graphics.Point();
        ADisplay.GetSize(size);
        int width = size.X;
        int height = size.Y;
        ActualTextSize = Convert.ToInt32(height * ATextSize / 100); // Convert percent to fraction and calculate actual height
        IsBold = AIsBold;
    }

    public override View GetDropDownView(int position, View convertView, ViewGroup parent) {                  
        TextView tv = (LayoutInflater.From(Context).Inflate(Resource.Layout.spinner_item, parent, false) as TextView);
        tv.SetTextSize(Android.Util.ComplexUnitType.Px, ActualTextSize);
        tv.SetText(GetItem(position).ToString(), TextView.BufferType.Normal);
        if ( IsBold ) {
            tv.SetTypeface(null, Android.Graphics.TypefaceStyle.Bold);
        }
        return tv;
    }

    public override View GetView(int position, View convertView, ViewGroup parent) {
        TextView tv = ( LayoutInflater.From(Context).Inflate(Resource.Layout.spinner_item, parent, false) as TextView );
        tv.SetTextSize(Android.Util.ComplexUnitType.Px, ActualTextSize);
        tv.SetText(GetItem(position).ToString(), TextView.BufferType.Normal);
        if ( IsBold ) {
            tv.SetTypeface(null, Android.Graphics.TypefaceStyle.Bold);
        }
        return tv;
    }
}

共有1个答案

燕嘉颖
2023-03-14

首先,您可以创建一个扩展ArrayAdapter的自定义适配器类。通过这种方式,您可以在内部对“Get DropDownView”进行覆盖,以便更改下拉弹出窗口的显示(注意,您需要定义“CustomSpinnerItem.xml”,在本例中只使用一个TextView ej textview1)

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
    View v = getLayoutInflater().inflate(R.layout.customspinneritem, parent, false);
    TextView tv = (TextView) v.findViewById(R.id.textview1);
    tv.setTextSize(**whatever**);
    tv.setText(languages[position]);
    return v;
}

如果您想在单击旋转器之前更改它的显示,要重写的一个函数是“获取视图(int position,View convertView,ViewGroup parent)”

 类似资料:
  • 问题内容: 我可以通过Java代码本身以编程方式设置java.library.path吗? 以下无效。 问题答案: 不,你不能。此属性是只读值。您可以在JVM启动时通过以下方式更改它: 如果要从特定位置加载库,则可以使用System.load(libraryPath)代替库的完整路径。

  • 尝试为Angular 7代码库设置Sonarqube以进行代码覆盖率分析。我已经安装了Sonarqube并在Admin中创建了一个项目,获得了授权cmd “sonar-scanner.bat-d”sonar.projectkey=testapp“-d”sonar.sources=.“-d”sonar.host.url=http://127.0.0.1:9000“-d”sonar.login=508

  • 我用eclipse创建了一个maven项目,并用下面的命令制作了jar文件 打包 当我尝试知道我的mvn项目配置是真的还是不是这个命令 mvn exec:Java-D exec . main class = " giraph . hello world . app " 我得到这个错误: 在helloworld项目上执行目标 org.codehaus.mojo: exec-maven-plugin:

  • 问题内容: 基本上,我在XML内有此功能,但是我必须在代码内重新创建它。我该怎么做? 我可以使用它来设置文本大小,但是layout_width和height呢? 有没有办法告诉代码使用DP单元而不是像素?还是将DP转换为像素的转换功能? 问题答案: 提取:

  • 问题内容: 我有一个JLabel,上面有很多文字。有没有一种方法可以使JLabel具有最大宽度,以便它包装文本以使其不超过该宽度? 谢谢 问题答案: 没有。 您可以在标签中使用HTML,但随后必须对break标签进行硬编码。 更好的方法是使用JTextArea并打开环绕。您可以更改文本的背景,前景,字体等,使其看起来像标签。 请注意,至少从Java 7开始,此答案已过时。 按照@darren的回答

  • 我正在使用代号为One的Eclipse插件。 现在我开始第二个项目。 我想设置一个或多个“片段”项目,以避免项目之间的代码重复。例如,如果我有一个用于字符串连接的实用程序类,我希望在一个实用程序片段项目中使用它,然后由我的项目引用。 使用Codename One的推荐方法是什么?