【问题标题】:Change textSize in a TextView在 TextView 中更改 textSize
【发布时间】:2018-06-08 09:19:42
【问题描述】:

在下面来自 android studio 上的 android 应用程序的代码中,我有一个 LinearLayout,其中包含一个 TextView,我想将其大小更改为例如从 100 到 20。但问题是,每当我更改 android :textSize="100.0dip" 到任何其他数字,在我的手机上运行时,应用程序中的大小仍然保持不变。

代码:

<ScrollView
    android:id="@id/qp_body_wrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/widget34"
    android:layout_below="@id/widget99"
    android:layout_marginBottom="5.0dip"
    android:layout_marginTop="10.0dip"
    android:clickable="true"
    android:focusable="true"
    android:foregroundGravity="right">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@id/qp_body_wrapper_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical">

        <TextView
            android:id="@id/qp_body"
            style="@style/QuoteViewText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/dashboard_qod_bg"
            android:clickable="true"
            android:focusable="true"
            android:gravity="right"
            android:padding="10.0dip"
            android:textAlignment="viewEnd"
            android:textColor="#FFFFFF"
            android:textSize="100.0dip"
            tools:ignore="RtlCompat,RtlHardcoded" />
    </LinearLayout>
</ScrollView>

这里是 QuoteViewText 样式:

<style name="QuoteViewText">
        <item name="android:textSize">100.0dip</item>
        <item name="android:typeface">serif</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#fffcfcfc</item>
        <item name="android:shadowColor">#ff102e46</item>
        <item name="android:shadowDx">0.0</item>
        <item name="android:shadowDy">1.0</item>
        <item name="android:shadowRadius">2.0</item>
    </style>

【问题讨论】:

  • 文本容器的大小是否相同?
  • 我是 android 和 java 新手,但我在这里编辑了代码,我复制了整个 scrollView ,这回答了你的问题?
  • 尝试用sp或者只用dp代替dip
  • 我已经这样做了,但没有任何改变:/
  • 天哪,要将其改回 20 dip(使用 sp 表示文本大小),您需要将 20 dip 放到 XML 中的 TextView 中。

标签: java android android-layout text-size


【解决方案1】:

您在 textsize 中使用 dip,而不是仅使用 dpsp。dp 和 sp 都可以工作,但在 textview 中正常,我们使用 sp

【讨论】:

    【解决方案2】:

    提示:使用dimens.xml 存储您的具体值,以便更轻松、更清晰地管理代码。

    除了从dip 更改为sptextSize 的多余双重设置之外,您的TextView 代码没有问题,它应该可以正常工作。还有一些其他小问题,可能使用 @+id/qp_body 和使用 match_parent 而不是已弃用的 fill_parent,但它仍然应该可以工作。

    这是在黑暗中拍摄,但请检查您是否没有以某种方式在代码中以编程方式覆盖该值。如果没有,您可以随时尝试看看会发生什么:

    TextView mTextView = (TextView) findViewById(R.id.qp_body);
    mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
    

    或者使用维度资源:

    TextView mTextView = (TextView) findViewById(R.id.qp_body);
    mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.my_text_size));
    

    【讨论】:

      【解决方案3】:

      您正在使用 dip ,而应该为 textSize 使用 sp

      您可以使用以下文件更新您的代码文件,它将解决您的问题

      <ScrollView
          android:id="@id/qp_body_wrapper"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_above="@id/widget34"
          android:layout_below="@id/widget99"
          android:layout_marginBottom="5.0dip"
          android:layout_marginTop="10dp"
          android:clickable="true"
          android:focusable="true"
          android:foregroundGravity="right">
      
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@id/qp_body_wrapper_container"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:clickable="true"
              android:focusable="true"
              android:orientation="vertical">
      
              <TextView
                  android:id="@id/qp_body"
                  style="@style/QuoteViewText"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/dashboard_qod_bg"
                  android:clickable="true"
                  android:focusable="true"
                  android:gravity="right"
                  android:padding="10dpp"
                  android:textAlignment="viewEnd"
                  android:textColor="#FFFFFF"
                  tools:ignore="RtlCompat,RtlHardcoded" />
          </LinearLayout>
      </ScrollView>
      

      这里是 QuoteViewText 样式:

       <style name="QuoteViewText">
                  <item name="android:textSize">100sp</item>
                  <item name="android:typeface">serif</item>
                  <item name="android:textStyle">bold</item>
                  <item name="android:textColor">#fffcfcfc</item>
                  <item name="android:shadowColor">#ff102e46</item>
                  <item name="android:shadowDx">0.0</item>
                  <item name="android:shadowDy">1.0</item>
                  <item name="android:shadowRadius">2.0</item>
              </style>
      

      【讨论】:

        【解决方案4】:

        文本大小以sp 衡量 - 与比例无关的像素。

        所以,在你的情况下,你应该使用

        <item name="android:textSize">100sp</item>
        

        改为

        <item name="android:textSize">100.0dip</item>
        

        另外,不要忘记样式被布局中的属性覆盖,在这种情况下,您应该从 TextView 中删除 android:textSize 并将其保留在样式中。此外,您应该从 10 的文本大小开始,100 很大并且从未使用过。

        【讨论】:

        • 我做了你在这里说的一切,但没有任何改变!!
        【解决方案5】:

        您将 textview 大小设置为 2 倍,一次来自应用样式,另一个来自为 textview 应用 textsize。使用任何一种。对于文本,使用 sp 而不是 dp 来尊重用户偏好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多