文本大小和不同的android屏幕大小
我知道,已经讨论过1000次了,但是我不能为不同的屏幕大小调整文本大小。我尝试在自定义样式中使用“sp”作为大小单位:文本大小和不同的android屏幕大小,android,screen-size,text-size,Android,Screen Size,Text Size,我知道,已经讨论过1000次了,但是我不能为不同的屏幕大小调整文本大小。我尝试在自定义样式中使用“sp”作为大小单位: <style name="CustumButtonStyle" parent="@android:style/Widget.Button"> ... <item name="android:textSize">30sp</item> ... </style> ... 30便士 ... 在2.7 QVG
<style name="CustumButtonStyle" parent="@android:style/Widget.Button">
...
<item name="android:textSize">30sp</item>
...
</style>
...
30便士
...
在2.7 QVGA中,它看起来还可以:
但在7英寸WSVGA中,它看起来是这样的:
我尝试使用“sp”和“dp”来获得相同的结果
你能解释一下如何使这些按钮在任何屏幕上看起来都一样吗
完全自定义按钮样式
<style name="CustumButtonStyle" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/custom_button</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">3dp</item>
<item name="android:textColor">#ffffff</item>
<item name="android:gravity">center</item>
<item name="android:textSize">30sp</item>
<item name="android:textStyle">bold</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">2</item>
</style>
@可绘图/自定义按钮
填补家长的空缺
包装内容
3dp
#ffffff
居中
30便士
大胆的
#000000
1.
1.
2.
在我的应用程序主题中
<item name="android:buttonStyle">@style/CustumButtonStyle</item>
@style/CustumButtonStyle
这就是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:background="@drawable/grid"
android:gravity="center"
android:orientation="vertical" android:layout_height="fill_parent">
<Button
android:id="@+id/buttonContinue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/continue_game" android:layout_marginTop="3dp" android:layout_marginBottom="3dp"/>
<Button
android:id="@+id/buttonNewGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonContinue"
android:layout_alignRight="@+id/buttonContinue"
android:layout_below="@+id/buttonContinue"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/new_game" android:layout_marginTop="3dp" android:layout_marginBottom="3dp"/>
<Button
android:id="@+id/ButtonAbout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonNewGame"
android:layout_alignRight="@+id/buttonNewGame"
android:layout_below="@+id/buttonNewGame"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/about" android:layout_marginTop="3dp" android:layout_marginBottom="3dp"/>
我对尺寸做了同样的处理,并绘制了类似的内容(使用dp,但仅用于文本和drawText()) XML:
我认为您可以通过为每个屏幕大小添加多个布局资源来存档,例如:
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size with small text
res/layout-large/my_layout.xml // layout for large screen size with larger text
res/layout-xlarge/my_layout.xml // layout for extra large screen size with even larger text
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
参考资料:
1.@forcelain我想你需要检查一下。在该pdf文件中,请转至第77页,在该页中,您将发现如何为不同的android设备使用dimens.xml,例如,请参见以下结构:
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-large/dimens.xml
res/values-xlarge/dimens.xml
例如,您在值中使用了以下dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
使用表2中的大小限定符,应用程序可以切换
在手机和平板电脑的不同布局资源之间使用
宽度和/或高度的任意数字。例如,如果600dp为
平板电脑布局支持的最小可用宽度,您可以
提供以下两组布局:
res/layout/main_activity.xml适用于手机
res/layout-sw600dp/main_activity.xml适用于平板电脑
在这种情况下,可用屏幕空间的最小宽度必须为
600dp,以便应用平板电脑布局
对于其他需要进一步自定义UI的情况
区分大小,如7英寸和10英寸平板电脑,您可以定义
其他最小宽度布局:
res/layout/main_activity.xml适用于手机(小于
600dp可用宽度)res/layout-sw600dp/main_activity.xml,适用于7“
平板电脑(600dp宽和更大)res/layout-sw720dp/main_activity.xml
适用于10英寸片剂(720dp宽和更大)
请注意,前两组示例资源使用
“最小宽度”限定符swdp,它指定
屏幕的两侧,无论设备的电流如何
方向。因此,使用swdp是指定
通过忽略屏幕的
方向
然而,在某些情况下,对布局可能重要的是
当前可用的确切宽度或高度。例如
如果您有两个并排包含两个片段的双窗格布局,则
可能希望在屏幕提供至少600dp的
宽度,无论设备是横向还是纵向。在里面
在这种情况下,您的资源可能如下所示:
res/layout/main_activity.xml适用于手机(小于
600dp可用宽度)res/layout-w600dp/main_activity.xml#
多窗格(任何可用宽度为600dp或更大的屏幕)
注意,第二组使用“可用宽度”限定符,
wdp。通过这种方式,一台设备可能实际使用两种布局,具体取决于
屏幕方向(如果可用宽度至少为
一个方向为600dp,另一个方向小于600dp
方向)
如果您关心可用高度,那么您也可以这样做
使用hdp限定符。或者,甚至结合wdp和hdp
限定符,如果你需要非常具体
不要硬编码尺寸 为了获得灵活性和新的屏幕分辨率-最佳做法是将虚拟TextView放入布局以获得textSize:
<TextView
android:id="@+id/dummyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="TextView" />
保留
textSize
作为参考,您可以添加常量或百分比大小(通过计算)。有时,最好只有三个选项
style="@android:style/TextAppearance.Small"
使用小屏幕和大屏幕以区别于正常屏幕大小
<TextView
android:id="@+id/TextViewTopBarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Small"/>
对于正常情况,您不必指定任何内容
<TextView
android:id="@+id/TextViewTopBarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
使用此功能,您可以避免测试和指定不同屏幕尺寸的尺寸。正如3.2(API级别13)中提到的@espinchi一样,不推荐使用尺寸组。
是最受欢迎的方法。我认为现在就回答这个问题已经太迟了。但我想和大家分享一下我的想法或方法,来解决不同分辨率设备上的文本大小问题。许多android开发者网站建议,我们必须使用sp单位来计算文本大小,这将处理不同分辨率设备的文本大小。但我总是无法得到想要的结果。因此,我已经找到了一个解决方案,我正在使用我的最后4-5个项目和它的工作良好。根据我的建议,您必须为每个分辨率设备放置文本大小,这有点繁琐,但它可以满足您的要求。每个开发人员都必须听取类似于4:6:8:12(分别为h:xh:xxh:xxxh)的比率。现在,在您的项目res文件夹中,您必须使用dimens文件创建4个文件夹,例如
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">4px</dimen>
</resources>
4px
对于其他分辨率,它类似于xhdpi:6px、xxhdpi:8px、xxxhdpi:12px。这是用我上面写的比率(3:4:6:8:12)计算出来的。让我们用一个例子来讨论其他文本大小的例子
style="@android:style/TextAppearance.Small"
<TextView
android:id="@+id/TextViewTopBarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Small"/>
<TextView
android:id="@+id/TextViewTopBarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">4px</dimen>
</resources>
<Button
app:autoSizeTextType="uniform" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:autoSizeTextType="uniform"
android:gravity="center_horizontal|bottom"
android:text="Your text goes here!"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.05"
app:layout_constraintHorizontal_bias="0.50"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.50"
app:layout_constraintWidth_percent="0.50" />
</androidx.constraintlayout.widget.ConstraintLayout>
app:layout_constraintWidth_percent="0.50"
app:layout_constraintHeight_percent="0.05"