Android的 - 在自定义样式未生效规定的浮动范围 [英] Android - margins specified in custom style not taking effect

查看:152
本文介绍了Android的 - 在自定义样式未生效规定的浮动范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有默认保证金的EditText的是10dp。因此,在我的styles.xml文件我设置如下:

 <资源的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <样式名称=MyTheme的父=机器人:款式/ Theme.NoTitleBar>
        <项目名称=机器人:editTextStyle> @风格/ edit_text_default< /项目>
    < /风格>

    <样式名称=edit_text_default父=机器人:款式/ Widget.EditText>
        <项目名称=机器人:layout_margin> 10dp< /项目>
    < /风格>

< /资源>
 

然后在AndroidManifest.xml中,我设置了应用程序的主题,我定义的:

 <应用
     机器人:图标=@可绘制/ ic_launcher
     机器人:标签=@字符串/ APP_NAME
     机器人:主题=@风格/ MyTheme的>
...
 

为主题的无标题栏方面的工作。但是,默认余量的EditText的不是,它仍然是填充所述父。这里是我的表视图:

 < TableLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:后台=#FFFFFF>
    <的TableRow>
        < EditText上的android:提示=@字符串/姓氏/>
    < /的TableRow>
    <的TableRow>
        < EditText上的android:提示=@字符串/ FIRST_NAME/>
    < /的TableRow>
< / TableLayout>
 

解决方案

简短的回答:如果您在自定义样式指定layout_margin,这种风格必须明确地应用到你希望每一个人观点有指定的保证金(为code样品图所示)。包括这种风格的主题,并把它应用到你的应用程序或活动将无法正常工作。

 < TableLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:后台=#FFFFFF>
    <的TableRow>
        < EditText上的android:提示=@字符串/姓氏的风格=@风格/ edit_text_default/>
    < /的TableRow>
    <的TableRow>
        < EditText上的android:提示=@字符串/ FIRST_NAME的风格=@风格/ edit_text_default/>
    < /的TableRow>
< / TableLayout>
 

说明:属性,它们都以布局_ 是<一href="http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html">LayoutParams,或者它的一个子类(如<一个href="http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html">MarginLayoutParams). 的LayoutParams 所使用的意见告诉他们的父母的ViewGroup 他们希望如何进行布局。每一个人都的ViewGroup 类实现扩展的嵌套类 ViewGroup.LayoutParams 因此,的LayoutParams 是特定于的ViewGroup 的类型。这意味着,当一个 TableLayout 的LinearLayout 可能都有 layout_margin 作为它的一个的LayoutParams ,它们被认为是完全不同的属性。

所以 layout_margin 是不是可以在任何地方应用只是一般的属性。它必须在的ViewGroup 的背景下,特别是将其定义为一个有效的论据内适用。视图必须的知道其父的类型的ViewGroup 的LayoutParams 应用。

指定layout_margin的风格,包括一个主题,风格,试图将这个主题应用到应用程序/活动将导致布局属性被丢弃,因为没有一个ViewGroup父已被指定尚未等参数无效。但是,运用样式已定义了 TableLayout 工作方式的EditText 视图,因为父的ViewGroup (即 TableLayout )是已知的。

来源:

布局参数

答给予这个问题由Android框架工程师和计算器用户的的=htt​​p://stackoverflow.com/users/342605/adamp> adamp

此外,给出的答复为这个问题由计算器用户的的 http://stackoverflow.com/users/69802/inazaruk">inazaruk

I wish to have the default margin for EditText's be 10dp. Therefore, in my styles.xml file I set up the following:

<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>

Then in AndroidManifest.xml, I set the applications theme to the one I defined:

<application
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:theme="@style/MyTheme" >
...

The "No Title Bar" aspect of the theme is working. However, the default margin for EditText's is not, it is still filling the parent. Here is my table view:

<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>

解决方案

Short Answer: If you are specifying layout_margin in a custom style, this style must be explicitly applied to each individual view that you wish to have the specified margin (as seen in the code sample below). Including this style in a theme and applying it to your application or an activity will not work.

<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>

Explanation: Attributes which begin with layout_ are LayoutParams, or one of its subclasses (e.g. MarginLayoutParams). LayoutParams are used by views to tell their parent ViewGroup how they want to be laid out. Each and every ViewGroup class implements a nested class that extends ViewGroup.LayoutParams. Therefore, LayoutParams are specific to the ViewGroup's type. What this means is that while a TableLayout and a LinearLayout may both have layout_margin as one of it's LayoutParams, they are considered to be completely different attributes.

So layout_margin is not just general attribute that can be applied anywhere. It must be applied within the context of a ViewGroup that specifically defines it as a valid argument. A view must be aware of the type of its parent ViewGroup when LayoutParams are applied.

Specifying layout_margin in a style, including that style in a theme, and attempting to apply that theme to an application/activity will result in the layout attributes being dropped, because no ViewGroup parent has been specified yet and so the arguments are invalid. However, applying the style to an EditText view that has been defined with a TableLayout works, because the parent ViewGroup (the TableLayout) is known.

Sources:

Android documentation on Layout Parameters.

Answer given to this question by Android framework engineer and StackOverflow user adamp.

Also, answer given to this question by StackOverflow user inazaruk.

这篇关于Android的 - 在自定义样式未生效规定的浮动范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆