Gro*_*ppe 27 android themes styles
我希望EditText的默认余量为10dp.因此,在我的styles.xml文件中,我设置了以下内容:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme" parent="android:style/Theme.NoTitleBar">
<item name="android:editTextStyle">@style/edit_text_default</item>
</style>
<style name="edit_text_default" parent="android:style/Widget.EditText">
<item name="android:layout_margin">10dp</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后在AndroidManifest.xml中,我将应用程序主题设置为我定义的主题:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
...
Run Code Online (Sandbox Code Playgroud)
该主题的"无标题栏"方面正在发挥作用.但是,EditText的默认边距不是,它仍然填充父级.这是我的表格视图:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<TableRow>
<EditText android:hint="@string/last_name" />
</TableRow>
<TableRow>
<EditText android:hint="@string/first_name" />
</TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)
Gro*_*ppe 50
简答:如果您在自定义样式中指定layout_margin,则必须将此样式显式应用于您希望具有指定边距的每个单独视图(如下面的代码示例所示).在主题中包含此样式并将其应用于您的应用程序或活动将不起作用.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<TableRow>
<EditText android:hint="@string/last_name" style="@style/edit_text_default" />
</TableRow>
<TableRow>
<EditText android:hint="@string/first_name" style="@style/edit_text_default" />
</TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)
说明:属性,其与开始layout_是的LayoutParams,或它的子类中的一个(例如MarginLayoutParams).LayoutParams视图使用它来告诉他们的父ViewGroup他们想要如何布局.每个ViewGroup类都实现了一个扩展的嵌套类ViewGroup.LayoutParams.因此,LayoutParams具体到ViewGroup的类型.这意味着虽然a TableLayout和a LinearLayout都可以layout_margin作为其中之一,但LayoutParams它们被认为是完全不同的属性.
因此layout_margin,不仅可以在任何地方应用的一般属性.它必须在ViewGroup专门定义为有效参数的上下文中应用.视图必须知道它的父的类型ViewGroup时LayoutParams施加的.
在样式中指定layout_margin(包括主题中的样式)并尝试将该主题应用于应用程序/活动将导致布局属性被删除,因为尚未指定ViewGroup父级,因此参数无效.但是,将样式应用于EditText已使用TableLayout作品定义的视图,因为父ViewGroup(the TableLayout)已知.
资料来源:
有关布局参数的 Android文档.
Android框架工程师和StackOverflow用户adamp 对此问题的回答.
此外,StackOverflow用户inazaruk回答了这个问题.
| 归档时间: |
|
| 查看次数: |
15816 次 |
| 最近记录: |