我需要在多个屏幕(xmls)中使用具有相同属性(如width,height,textColor,size等)的相同小部件(按钮,editext,textviews).所以我为各个小部件创建了样式(一个用于按钮,一个用于编辑文本......)我在CustomTheme中定义了所有这些样式.
我的问题是
如果我也在样式中定义布局宽度/高度,并简单地给出style ="@ style/myButtonStyle"for xml中的按钮工作正常,即使我没有提到xml中的宽度/高度(可能继承自Style).
如果我在没有样式道具的情况下给出xml中的宽度/高度,只需将我的自定义主题赋予活动就会带来我在主题中指定的所有样式.但我没有提到xml中的宽度/高度,它提出了一个异常,说你已经指定了布局宽度/高度,我已经在样式本身中指定了它.
我的主题文件是
<style name="Theme.Blue" parent="android:Theme">
<item name="android:buttonStyle">@style/button222</item>
<item name="android:textViewStyle">@style/text222</item>
<item name="android:editTextStyle">@style/edittext222</item>
</style>
Run Code Online (Sandbox Code Playgroud)
而我的button222风格是
<style name="button222" parent="@android:style/Widget.Button">
<item name="android:layout_width">@dimen/mWidth</item>
<item name="android:textColor">#F56266</item>
<item name="android:textSize">20sp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我指定尺寸为
<dimen name="mWidth">180dip</dimen>
Run Code Online (Sandbox Code Playgroud)
我在layout.xml中使用了这个
<Button android:layout_width="180dip"
android:layout_height="wrap_content"
android:text="This is large text."
/>
<Button
android:layout_height="wrap_content"
android:text="This is regular text one"
/>
<Button
style="@style/button222"
android:layout_height="wrap_content"
android:text="This is regular text."
/>
Run Code Online (Sandbox Code Playgroud)
它给出了例外说明布局宽度的异常,我在button222样式中提到并试图通过我的主题使用.
小智 10
您应该在样式xml中插入维度值:
<dimen name="buttonHeight">40px</dimen>
Run Code Online (Sandbox Code Playgroud)
那么你只需要引用维度值.所以在你的布局文件中你会写:
android:layout_height="@dimen/buttonHeight"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12193 次 |
| 最近记录: |