2

我对按钮中的文本大小有疑问。我有使用 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>

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

4

4 回答 4

3

使用 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 所建议的那样,但同时考虑这一点

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

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-05-15T07:49:59.277 回答
1

如果您在按钮中添加 XML:

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

这应该可以解决问题。

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

于 2012-05-15T07:15:08.437 回答
1

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

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

有关更多信息,您可以参考此链接:

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

于 2012-05-15T07:21:24.957 回答
-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" />
于 2012-05-15T07:23:40.090 回答