简体   繁体   English

如何获取在Textview中设置的字体大小

[英]How to get the font size set in the Textview

I am using the typeface in my text view and it is all looking good. 我在文本视图中使用了字体,看上去一切都很好。 Now I want to set the seekbar which will help the user to adjust the text size under the text view. 现在,我想设置搜索栏,它将帮助用户在文本视图下调整文本大小。

I have just used the seekbar and played with it. 我刚刚使用了搜索栏并进行了操作。 this is how I am using my Seekbar to make the text larger 这就是我使用Seekbar放大文本的方式

/// SeekBar Progress

    sk.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int p=0;


        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            if(p<30)
            {
                p=30;
                sk.setProgress(p);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
            // TODO Auto-generated method stub
            p=progress;
            myTextView.setTextSize(p);
        }
    });

this is working and changing the size of text in the text view, But now there comes the problem : 这正在工作并且正在更改文本视图中文本的大小,但是现在出现了问题:

Problem : 问题:

Problem is , the size is increasing and decreasing also when we take the seek bar to the left from its mid point. 问题是,当我们将搜索栏从中间点向左移时,大小也在增加和减小。 where as I want to set the minimum and maximum value. 我想在哪里设置最小值和最大值。 So to set the minimum and maximum I need to get the size of text inside the text view . 因此,要设置最小值和最大值,我需要获取文本视图内文本的大小。 and I have not found any method to get the textsize like for example myTextview.getTextSize() , there is no such method like it. 而且我还没有找到任何方法来获取文本大小 ,例如myTextview.getTextSize() ,没有这样的方法。

Let me tell you I am setting the size of the text view from the dimens file in the different values folder for supporting all the device and to make my text look good on tablets and small devices. 让我告诉您,我正在其他值文件夹中的dimens文件中设置文本视图的大小,以支持所有设备,并使我的文本在平板电脑和小型设备上看起来不错。

So now All I need to get is My text size inside the Text view . 因此,现在我需要获取的是“文本”视图中的“我的文本大小”。 Please help me in getting the text size. 请帮助我获取文字大小。 thank you. 谢谢。

There is TextView.getTextSize() . TextView.getTextSize() You can also go TextView.getPaint().getTextSize() . 您也可以转到TextView.getPaint().getTextSize()

try this 尝试这个

text_one.setText("your text");
text_one.measure(0, 0);       //must call measure!
text_one.getMeasuredHeight(); //get height
text_one.getMeasuredWidth(); 

Do check that your myTextView is indeed TextView and not just View when you initialize it. 初始化时,请检查myTextView确实是TextView ,而不仅仅是View eg 例如

TextView myTextView = (TextView) findViewById(yourTextViewId);

I think the simplest answer is 我认为最简单的答案是

myTextView.getTextSize();

Actually I was casting my value wrong. 实际上,我在错误地表达我的价值观。 and Now it is solved. 现在解决了。 after doing this 在这样做之后

 float size = myTextView.getTextSize();

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

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