【问题标题】:Providing a default style (attributes) in a custom view在自定义视图中提供默认样式(属性)
【发布时间】:2012-05-21 08:33:56
【问题描述】:

请告诉我,如何将自定义按钮的默认背景设置为 null。

我的意思是... 我知道我可以定义一个“样式”,将 android:background 设置为“@null”, 并要求用户在其布局中明确应用样式。例如:

<style name="MyButton" parent="@android:style/Widget.Button">
    <item name="android:background">@null</item>
</style>

<com.xxx.widget.MyButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/MyButton"
    android:text="MyButton" />

以上代码运行良好。 但是我怎样才能在我的类“MyButton”内部应用这种样式,让用户不要明确设置样式呢?

例如,如何使以下布局像以前一样工作:

<com.xxx.widget.MyButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="MyButton" />

我尝试在下面的构造函数中执行此操作,但它不起作用。

public MyButton(Context context, AttributeSet attrs) {
    this(context, attrs, com.xxx.R.style.MyButton);
}

PS。当用户未设置背景时,我想应用此“空”背景 明确的。

【问题讨论】:

标签: android view background widget styles


【解决方案1】:

在您的 MyButton() 构造函数中,为什么不调用 setBackground(null)?

【讨论】:

    【解决方案2】:

    在这里参加聚会真的很晚了。但如果有人偶然发现同样的问题,这是我的解决方案。

    假设我们想要一个扩展TextInputLayout 的自定义视图。以下是该类的典型外观:

    class MyTextInputLayout: TextInputLayout {
        constructor(context: Context) : this(context, null)
        constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
        constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
            : super(context, attrs, defStyleAttr)
    }
    

    现在,为了使用我们的自定义样式作为默认样式,将第三个构造函数(具有三个参数的构造函数)更改为:

    class MyTextInputLayout: TextInputLayout {
        constructor(context: Context) : this(context, null)
        constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
        constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
            : super(ContextThemeWrapper(context, R.style.MyTextInputLayoutStyle), attrs, defStyleAttr)
    }
    

    现在我们可以像这样在布局 XML 文件中使用自定义视图:

    <com.xxx.MyTextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    

    我们不再需要在组件中定义style="@style/MyTextInputLayoutStyle"

    简而言之,我们需要将传递给超级构造函数的context 替换为包装我们样式的那个。这样我们就不需要在现有主题中定义 R.attr.MyTextInputLayoutStyle 了。当您想将自定义视图用作库时很有用。

    【讨论】:

    • 这对我有用,谢谢先生
    • 您不应该使用默认参数生成构造函数(即this(context, attrs, 0),因为0 将解析为意外样式。删除0。
    【解决方案3】:

    这或多或少是一种便宜的方法,但为什么不将背景设置为透明正方形。像这样->

    transparent_square.xml:(放到drawable目录下)

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/square"
        android:>
    
        <solid android:color="#00808080"></solid> // The first two zeros set the alpha
                                                  // to 0
    
    </shape>
    

    然后将按钮的背景设置为您的可绘制对象。

    按钮:

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send"
            android:id="@+id/sendButton"
            android:layout_weight="1"
            android:background="@drawable/transparent_square"/>
    

    【讨论】:

    • 对不起,但我认为这并不能回答问题。 @Henry 询问默认情况下如何执行此操作,而无需在 Button 的 xml 中指定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多