eclipse 为什么我不应该在样式中设置 layout_width 和 layout_height?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16497758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:28:51  来源:igfitidea点击:

Why shouldn't I set layout_width and layout_height in a style?

androideclipseandroid-layoutadt

提问by Axarydax

If I have a lot of TextViews serving as item labels, I thought I could extract everything that's common about them into a style, and in the layout use only

如果我有很多 TextViews 作为项目标签,我想我可以将它们的所有共同点提取到一个样式中,并且仅在布局中使用

<TextView style="@style/label" android:text="Foo"/>
<TextView style="@style/label" android:text="Bar"/>

with style like:

风格如:

<style name="label">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

But when I do this, it works fine on emulator and device, but the Android XML editor complains that "<TextView>" does not set the required layout_height attribute.Is there some reason on why it is reporting this warning?

但是当我这样做时,它在模拟器和设备上运行良好,但 Android XML 编辑器抱怨它"<TextView>" does not set the required layout_height attribute.为什么报告这个警告有什么原因吗?

回答by Yoann Hercouet

Have you tried to clean the project?

您是否尝试过清理项目?

I am using also the same thing on some XML files for the width and the length. Here is an example working on my app, you can have a look:

我也在一些 XML 文件上使用相同的宽度和长度。这是在我的应用程序上运行的示例,您可以查看:

<TextView
    android:id="@+id/viewdescription"
    android:textIsSelectable="true"
    android:layout_below="@+id/rating_bar_view"
    android:minLines="8"
    style="@style/description" />

And the XML:

和 XML:

<style name="description">
   <item name="android:layout_gravity">center_horizontal</item>
   <item name="android:inputType">textMultiLine</item>
   <item name="android:layout_width">fill_parent</item>
   <item name="android:layout_height">wrap_content</item>     
   <item name="android:textSize">14sp</item>
   <item name="android:textColor">@color/Black</item>
   <item name="android:layout_marginRight">8dp</item>
   <item name="android:layout_marginLeft">8dp</item>

回答by jterry

After a Build > Clean Projectattempt, I was still experiencing this same issue in the layout editor; a full restart of my IDE solved the problem for me.

经过Build > Clean Project尝试,我仍然在布局编辑器中遇到同样的问题;完全重启我的 IDE 为我解决了这个问题。

回答by bio007

Old topic but still :)

老话题,但仍然:)

I don't think setting a width and height to style is a good idea. Basically it compiles and usually works but I experienced situations with more complex layouts, includes etc. where it was causing problems. And when you think about it, it doesn't really make sense to put it there.

我不认为为样式设置宽度和高度是一个好主意。基本上它可以编译并且通常可以工作,但我遇到了更复杂的布局、包含等导致问题的情况。当你考虑它时,把它放在那里真的没有意义。

You can easily use @dimen if you want different values for different layouts. And it can also be pretty surprising for somebody else using your style. You usually want to set color, size, and behavior with style but size is not one of the things.

如果您想要不同布局的不同值,您可以轻松使用 @dimen。对于使用您风格的其他人来说,这也可能令人惊讶。您通常希望使用样式设置颜色、大小和行为,但大小不是其中之一。

You never know in what context your style gonna be used in. It's just one of these smelly things which when you see you start to have a feeling that something could have been done better.

你永远不知道你的风格会被用在什么环境中。这只是这些臭东西之一,当你看到你开始觉得有些事情可以做得更好。

回答by Raja Jawahar

I have tried this, its just working fine..

我试过这个,它工作正常..

 <EditText
        android:id="@+id/edt_mobile_no"
        style="@style/login_input_fields"
        android:hint="@string/hint_mobile"
        android:inputType="phone" />

Here below the style for the EditText

下面是 EditText 的样式

<style name="login_input_fields">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">40dp</item>
    <item name="android:layout_marginBottom">10dp</item>
    <item name="android:background">@drawable/bg_curved_white_border</item>
    <item name="android:paddingRight">10dp</item>
    <item name="android:paddingLeft">10dp</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textColorHint">@color/underline</item>
    <item name="android:textSize">16sp</item>
    <item name="android:typeface">monospace</item>
</style>

回答by Foad Saeidi Nik

I thing you should put the parent of label as "@android:style/Widget.TextView"

我认为您应该将标签的父级设置为“@android:style/Widget.TextView”