android - 覆盖 TextView 的文本大小

标签 android android-layout android-activity

我想以编程方式将 UserSetting 中的文本大小设置为我的 textView;问题是当它随着 myTextView.setTextSize(18) 改变时,一旦我返回到我的 xml,它就会返回默认大小,我不想要那个,而是我想保留我自己的文本大小。

我的 TextView xml:

<TextView
                        android:id="@+id/MainText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/spacing_large"
                        android:layout_marginRight="@dimen/spacing_large"
                        android:layout_marginTop="15dp"
                        android:lineSpacingExtra="3dp"
                        android:text="long_lorem_ipsum_2"
                        android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
                        android:textColor="@color/grey_60"
                        android:layout_marginBottom="80dp"/>

最佳答案

使用 SharedPreferences 来保存你自己的 TextSize

在 SharedPreferences 中保存文本大小的代码:

  SharedPreferences  pref=getSharedPreferences("MyPref",MODE_PRIVATE);              
   SharedPreferences.Editor editor=pref.edit();            
   editor.putFloat(FONT_SIZE_KEY,textSize);    // your textsize       
   editor.commit();

从 SharedPreferences 中获取 textsize 的值

       SharedPreferences prefs=getSharedPreferences("MyPref", MODE_PRIVATE);
       float fontsize=prefs.getFloat(FONT_SIZE_KEY, 12);

关于android - 覆盖 TextView 的文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53950004/

相关文章:

android - 如何在ListView内的ImageView中设置OnClickListener?

java - 找不到符号变量activity_main

android - 安卓中的VSYNC是什么

java - 软键盘切断了我的 EditText/TextInputEditText 控件的底部

android - 设置一个 ksoap 请求

java - 使用 Chrome 自定义选项卡时如何捕获错误

android - 在同一屏幕上显示两个 Activity

android - 如何在 ActionBar 中引入 Swipeable Tabs?

android - 借助 onRetainNonConfigurationInstance() 将位图保存在内存中

android - 在 Activity 和 ViewPager fragment 之间进行通信