Liz*_*ica 5 android android-layout
我为大屏幕上的所有文本按钮制作了自定义样式
<style name="button_large" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/button_color</item>
<item name="android:textSize">30dp</item>
<item name="android:clickable">true</item>
<item name="android:soundEffectsEnabled">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)
现在我想要正常屏幕的小按钮,我添加noew样式,只有android:textSize更改.
<style name="button_normal" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/button_color</item>
<item name="android:textSize">10dp</item>
<item name="android:clickable">true</item>
<item name="android:soundEffectsEnabled">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)
是否可以提取此值并根据屏幕大小接收所需的值?并且只使用一种风格.
<item name="android:textSize">value_based_on_screen_size</item>
Run Code Online (Sandbox Code Playgroud)
在大屏幕的情况下,它将为30,对于普通屏幕,它将为10
Jef*_*man 10
是的,这是可能的.以下是您可以安排的方式.在你的默认values/styles.xml
有,
<style name="button" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/button_color</item>
<item name="android:textSize">@dimen/button_text_size</item>
<item name="android:clickable">true</item>
<item name="android:soundEffectsEnabled">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后创建一个values/dimens.xml
这样的,
<resources>
<dimen name="button_text_size">10sp</dimen>
</resources>
Run Code Online (Sandbox Code Playgroud)
并创建一个values-large
这样的,
<resources>
<dimen name="button_text_size">30sp</dimen>
</resources>
Run Code Online (Sandbox Code Playgroud)
Ps,sp
用于字体.它就像是dp
,除非用户通过系统设置增加默认字体大小,否则你的字体会相应改变.