【问题标题】:Button and size of text按钮和文本大小
【发布时间】:2012-05-22 16:09:22
【问题描述】:

我对按钮中的文本大小有疑问。我有使用 weightSum 的布局,我的按钮宽度与父级匹配,高度为 30%。一切正常,但是当我在模拟器上更改屏幕大小时,我的文本仍然很小。如何根据屏幕大小更改文本大小? 这是xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_weight="30" />

</LinearLayout>

有什么方法可以自动做到这一点?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    如果在按钮中添加XML:

    <Button
        android:id="@+id/button1"
        android:text="click me"
        android:textSize="22dp" />
    

    这应该可以解决问题。

    如果你想动态改变文字大小,你应该得到屏幕大小,然后用button.setTextSize(size);设置textSize

    【讨论】:

      【解决方案2】:

      对于不同的屏幕,如果你想让 UI 查找所有屏幕,那么 Android 中有不同的布局,那么你必须为布局创建 4 个文件夹

      1) layout
      2) layout-small
      3) layout-large
      4) layout-xlarge
      

      更多信息你可以参考这个链接:

      http://developer.android.com/guide/practices/screens_support.html

      【讨论】:

        【解决方案3】:
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button"
            android:textSize="22dp"
            android:layout_weight="30" />
        

        【讨论】:

          【解决方案4】:

          使用sp作为按钮文本大小的单位

          <Button
          android:id="@+id/button1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Button"
          android:textSize="20sp"
          android:layout_weight="30" />
          

          如果这不能解决您的问题,请创建文件夹

          1) 布局 2) 布局-小 3) 布局-大 4) 布局-xlarge

          按照 Nikhil 的建议,但同时考虑 this

          与上面建议的文件夹相同,您可以为每种屏幕类型创建不同的值文件夹

          1) 值 2) 值-小 3) 值-大 ...

          在每个文件中创建一个名为dimens.xml的xml文件,并使用dimen资源的引用指定按钮的textSize,如下所示

          <Button
          android:id="@+id/button1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Button"
          android:textSize="@dimen/small_font"
          android:layout_weight="30" />
          

          这将根据屏幕大小设置字体大小

          希望对你有用

          【讨论】:

            猜你喜欢
            • 2012-09-20
            • 2016-05-27
            • 1970-01-01
            • 2022-10-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多