如何使用 AppCompat 库创建自定义按钮?
How to create custom Button with using AppCompat library?
是否可以在不丢失 coloring/tinting/shadows 等 AppCompat 功能的情况下扩展 Button class?
现在,如果我创建自定义 class 扩展 Button 并在布局中使用它,它会变成白色 background/black 前景。
例如自定义按钮:
package me.shikhov.buttontest;
import android.content.Context;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
/**
* Created by andrew on 19.01.16.
*/
public class MyButton extends
AppCompatButton
{
public MyButton(Context context) {
this(context, null);
}
public MyButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
示例布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="me.shikhov.buttontest.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST BUTTON"
android:layout_centerInParent="true"
android:id="@+id/standard_button"
/>
<me.shikhov.buttontest.MyButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/Widget.AppCompat.Button"
android:text="MY BUTTON TEST"
android:layout_centerHorizontal="true"
android:layout_below="@id/standard_button"
/>
</RelativeLayout>
重要提示:
MyButton 应该扩展 AppCompatButton
而 不是 android.view.Button
您应该明确设置android:theme
。该主题应源自 Widget.AppCompat.Button
theme.
是否可以在不丢失 coloring/tinting/shadows 等 AppCompat 功能的情况下扩展 Button class?
现在,如果我创建自定义 class 扩展 Button 并在布局中使用它,它会变成白色 background/black 前景。
例如自定义按钮:
package me.shikhov.buttontest;
import android.content.Context;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
/**
* Created by andrew on 19.01.16.
*/
public class MyButton extends
AppCompatButton
{
public MyButton(Context context) {
this(context, null);
}
public MyButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
示例布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="me.shikhov.buttontest.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST BUTTON"
android:layout_centerInParent="true"
android:id="@+id/standard_button"
/>
<me.shikhov.buttontest.MyButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/Widget.AppCompat.Button"
android:text="MY BUTTON TEST"
android:layout_centerHorizontal="true"
android:layout_below="@id/standard_button"
/>
</RelativeLayout>
重要提示:
MyButton 应该扩展
AppCompatButton
而 不是android.view.Button
您应该明确设置
android:theme
。该主题应源自Widget.AppCompat.Button
theme.