2014-02-20 71 views
1

我想将默认layout_width = wrap_contentlayout_height = wrap content设置为我所有的TextView s。于是,我就修改与主题文件:TextView默认布局参数的主题

<item name="android:textViewStyle">@style/TextViewStyle</item> 

<style name="TextViewStyle" parent="@android:style/Widget.TextView"> 
    <item name = "android:layout_width">wrap_content</item> 
    <item name = "android:layout_height">wrap_content</item> 
</style> 

但如果我设置了TextView没有layout_width或不layout_height,应用程序在执行时停止。

它可以按我提出的方式完成吗?大项目可以节省很多xml“无用”的线。

回答

0

不幸的是,无法从主题设置布局属性(layout_ *)(请参见Layout Parameters documentationthis answer from an Android framework engineer)。但是,您可以设置样式布局属性,让每一个元素引用这样的风格:

<Button style="@style/ButtonBig" android:text="my text" /> 

其中ButtonBig这样定义:

<style name="ButtonBig" parent="android:Widget.Holo.Light.Button"> 
    <item name="android:textSize">20sp</item> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">100dp</item>   
</style>