要在作为主题应用的按钮样式中使用layout_marginLeft吗?

kik*_*iki 28 android themes coding-style margins button

layout_marginLeft="30dip"在为按钮定义的样式中使用了属性.当我为每个按钮单独应用此样式时,左边距按我的意愿放置.

但后来我定义了一个主题,我将按钮样式分配给属性android:buttonStyle并将其应用到我的项目中.
但是,左边距属性将被忽略.知道我必须在这做什么吗?

style.xml如下:

<style name="btnstyle" parent="@android:style/Widget.Button">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_marginLeft">30dip</item>
    <item name="android:textColor">#FFFFFF</item>       
</style> 

<style name="appstyle" parent="android:Theme"> 
    <item name="android:buttonStyle">@style/btnstyle</item> 
</style>
Run Code Online (Sandbox Code Playgroud)

Fra*_*oid 13

看起来这可以通过在形状周围包裹insets标签来实现.

像这样:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="5dp"
    android:insetRight="5dp"
    android:insetTop="5dp"
    android:insetBottom="5dp">

<shape  
    android:shape="rectangle">
    <solid 
        android:color="#fff"/>
    <stroke
        android:width="1dp"
        android:color="#333"/>
    <corners 
        android:radius="10dp"/>
</shape>
</inset>
Run Code Online (Sandbox Code Playgroud)


War*_*zit 10

stackoverflow上有一个很棒的答案.基本上LayoutParams(layout_)用于子视图,告诉他们的父母ViewGroup应该如何定位.但LayoutParams具体到每个ViewGroup类型(LinearLayout,RelativeLayout,FrameLayout等),所以LayoutParams不能以这种方式为主题一概而论.

所有这一切都非常不幸,因为你不能在主题中使用完全通用的样式,但你仍然可以在你需要的每个特定视图中使用该样式,如下所示:

<Button style="@style/btnstyle"/>
Run Code Online (Sandbox Code Playgroud)


Shi*_*yUK 3

有没有搞清楚这件事的真相?我也有同样的问题;通过主题内的样式应用于 Button 或 ImageButton 时,所有边距属性都会被忽略。

但是,我发现当使用按钮上的“style”属性直接应用样式时,它确实有效。似乎边距不是“buttonStyle”规范的一部分。