[Android]TextView设置字体大小时应该知道的事-同样的textSize不同的效果

在学helloworld的时候就第一个接触的控件就是TextView,这个是非常常用的一个文本控件,现在我们要说的就是关于设置大小时应该清楚的一两个问题.下面这个案例来说明.

先看xml中的案例:

<LinearLayout
        
        android:layout_below="@+id/main_button_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textview_1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:text="字体20sp" />
        
        <TextView
            android:id="@+id/textview_2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:text="字体20px" />
    </LinearLayout>
效果图:


好了,看效果图就知道本博文主要讲的就是sp和px的一个不清楚的情况下 出现 大小不如所愿的问题.

代码案例:

1.dimens.xml

 <dimen name="text_size_20">20sp</dimen>
2.在java中出现大小不一样的问题的原型:

		TextView textView1 = (TextView) findViewById(R.id.textview_1);
		textView1.setTextSize(20);//默认单位其实是sp
		
		TextView textView2 = (TextView) findViewById(R.id.textview_2);
		float size = this.getResources().getDimension(R.dimen.text_size_20);
		Log.d(TAG, "textSize = "+size); //30
		textView2.setTextSize(size); //这里的size单位是px,即30个像素 
		
3.看看TextView中的setTextSize方法 就能看出问题了.

怎么改,现在你应该知道了吧?

在后面追加修改成:

textView2.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);

Ok,这里其实是基础的东西.单位分清楚其实也很重要哦. 大笑


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在布局文件中为 TextView 设置字体: ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textSize="20sp" android:fontFamily="sans-serif" /> ``` 在代码中为 TextView 设置字体: ```java TextView textView = findViewById(R.id.text_view); textView.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf")); ``` 如果你的字体文件在 assets/fonts/ 目录下,并且文件名为 myfont.ttf 。 Android 8.0 以上可以使用以下方式设置 ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textSize="20sp" android:fontFamily="@font/myfont" /> ``` 然后将字体文件放入 res/font/ 目录下,并命名为 myfont.ttf ``` ### 回答2: 在Android开发中,TextView是常用的控件之一,通过TextView可以展示文本信息。但是有候,我们需要自定义字体,以便更好地适应我们的设计需求和风格。 那么如何在TextView设置自定义字体呢?下面详细介绍: 首先,首先你需要准备字体文件,一般字体文件的格式是.ttf或.otf,必须放在res/font或assets文件夹下。 接着,我们需要定义一个自定义字体的文件夹。如果字体文件被放在res/font下,则需要在res文件夹下定义一个font文件夹。如果字体文件被放在assets下,则需要在main文件夹下定义一个fonts文件夹。 定义完字体文件夹后,我们可以在TextView中通过代码来设置字体。主要有以下几个方法: 1. setTypeface方法:通过该方法设置TextView的字体,可以选择系统自带的字体,也可以选择自定义字体。 示例代码: //设置TextView的字体为系统自带的sans-serif-bold textView.setTypeface(Typeface.DEFAULT_BOLD); //设置TextView的字体为自定义字体 Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/impact.ttf"); textView.setTypeface(tf); 2. setTextAppearance方法:该方法可以通过指定样式来设置TextView的字体,样式可以是系统样式,也可以是自定义样式。 示例代码: //使用系统提供的样式为TextView设置字体 textView.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_Large); //定义自定义样式 <style name="CustomTextStyle"> <item name="android:textSize">20sp</item> <item name="android:textColor">#666666</item> <item name="android:fontFamily">@font/impact</item> </style> //使用自定义样式为TextView设置字体 textView.setTextAppearance(R.style.CustomTextStyle); 3. 在布局文件中设置字体:可以在布局文件中使用TextView标签中的android:fontFamily属性来指定字体。 示例代码: <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:fontFamily="@font/impact" /> 总之,以上是在Android设置TextView的自定义字体的几种方法,可以根据实际情况进行选择使用。此外,我们还可以使用SpannableStringBuilder来设置富文本样式,包括字体样式。 ### 回答3: Android开发中,TextView是常用的控件之一。我们可以通过代码或xml文件来设置TextView的属性,包括字体属性。下面我们就来讲讲如何设置TextView字体。 一、使用字体文件 1. 在assets目录下创建一个fonts目录,把字体文件放在fonts目录下,如assets/fonts/MyFont.ttf。 2. 在res目录下创建一个xml目录,创建一个名为font_path.xml的文件。 3. 在文件中添加以下代码: ``` <?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:font="@font/MyFont" android:fontStyle="normal" android:fontWeight="400" /> </font-family> ``` 4. 在xml文件中设置TextView的字体,代码如下: ``` <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:fontFamily="@font/font_path" /> ``` 二、使用系统字体 1. 在xml文件中设置TextView的字体,代码如下: ``` <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:fontFamily="sans-serif" /> ``` 2. 如果要使用其他系统字体,可以在fontFamily属性中设置字体名称,如: ``` android:fontFamily="serif" ``` 三、使用第三方库 1. 在app/build.gradle文件中的dependencies添加以下代码: ``` dependencies { implementation 'uk.co.chrisjenx:calligraphy:2.3.0' } ``` 2. 在Application中初始化Calligraphy,代码如下: ``` @Override public void onCreate() { super.onCreate(); CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() .setDefaultFontPath("fonts/MyFont.ttf") .setFontAttrId(R.attr.fontPath) .build()); } ``` 3. 在xml文件中设置TextView的字体,代码如下: ``` <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:fontPath="@string/font_path" /> ``` 以上就是几种设置TextView字体的方法,大家可以根据自己的需求选择适合自己的方法,希望对大家有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值