扩展AppCompatButton的自定义按钮

时间:2018-09-01 21:37:23

标签: java android

我正在尝试创建将包含多个TextView的自定义按钮。我读到我应该让它扩展android.support.v7.widget.AppCompatButton而不是android.widget.Button

这是自定义按钮类的代码:

public class CustomButton extends AppCompatButton {

    public CustomButton(Context context) {
        this(context, null);
    }
    public CustomButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public CustomButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
}

这是一个简单的按钮,但是,一旦我在活动xml中添加textview,我就会收到以下错误,其中错误行是文本视图的行

  

java.lang.RuntimeException:无法启动活动ComponentInfo {me.app/me.app.MainActivity}:android.view.InflateException:二进制XML文件第21行:me.app.CustomButton无法转换为android。 view.ViewGroup

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <me.app.CustomButton
        android:id="@+id/button7"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="100dp"
        android:text="Button"
        android:theme="@style/Widget.AppCompat.Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
        android:id="@+id/increment_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="00:00:00"
        android:textColor="@android:color/black"
        android:textSize="22sp" />

    </me.app.CustomButton>
</android.support.constraint.ConstraintLayout>

如果我扩展AppCompatButton而不是Button,是否需要以不同的方式编写activity.xml文件?

1 个答案:

答案 0 :(得分:0)

您可以使用CardView获得相同的效果...

<android.support.v7.widget.CardView
        android:id="@+id/card0"
        android:focusable="true"
        android:clickable="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:foreground="?android:selectableItemBackground"
        app:cardCornerRadius="4dp"
        app:cardElevation="4dp"
        app:cardPreventCornerOverlap="false">

        <LinearLayout
            android:id="@+id/ll1"
            android:padding="10.0dip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/t1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="How to Refresh!?"
                android:textSize="16sp"
                android:textStyle="bold"
                android:typeface="serif"
                android:gravity="center"/>

            <TextView
                android:id="@+id/t2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="How to Refresh!?"
                android:textSize="16sp"
                android:textStyle="bold"
                android:typeface="serif"
                android:gravity="center"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>

这样,您可以添加任意数量的TextView!

相关问题