不同API中的自定义按钮

时间:2017-07-07 10:43:02

标签: android button

我做了一个自定义按钮类来制作方形按钮。按钮在牛轧糖中效果很好,但在棒棒糖和棉花糖中没有显示。我的代码出了什么问题?

Java类:

public class SquareButton extends ToggleButton {

        public SquareButton(Context context) {
            super(context);
        }

        public SquareButton(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public SquareButton(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);
            int size = width > height ? height : width;
            setMeasuredDimension(size, size);

        }
    }

XML:

    <android.support.constraint.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="ru.energyhouse.energyhouse.presenter.Light.MainMenuLightFragment"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <ImageView
        android:id="@+id/main_menu_light_icon_IV"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="0dp"
        android:adjustViewBounds="true"
        android:cropToPadding="false"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toTopOf="@+id/guideline11"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline9"
        app:srcCompat="@drawable/light_lightoff_big_ic" />

    <TextView
        android:id="@+id/main_menu_light_icon_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="@color/colorPrimary"
        app:layout_constraintLeft_toLeftOf="@+id/main_menu_light_icon_IV"
        app:layout_constraintRight_toRightOf="@+id/main_menu_light_icon_IV"
        app:layout_constraintTop_toTopOf="@+id/guideline11"
        app:layout_constraintHorizontal_bias="0.5" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline9"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.25" />

    <HorizontalScrollView
        android:id="@+id/main_menu_light_scroll"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="16dp"
        android:overScrollMode="never"
        android:scrollbars="none"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_menu_light_icon_TV"
        app:layout_constraintVertical_bias="0.0">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="2">

            <LinearLayout
                android:id="@+id/main_menu_light_firstRow"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ru.energyhouse.energyhouse.model.SquareButton
                    android:id="@+id/main_menu_light_bright_Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/scenes_click"
                    android:paddingTop="45dp"
                    android:textOff="@null"
                    android:textOn="@null" />

                <ru.energyhouse.energyhouse.model.SquareButton
                    android:id="@+id/main_menu_light_soft_Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/scenes_click"
                    android:paddingTop="45dp"
                    android:textOff="@null"
                    android:textOn="@null" />

                <ru.energyhouse.energyhouse.model.SquareButton
                    android:id="@+id/main_menu_light_manual_Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/scenes_click"
                    android:paddingTop="45dp"
                    android:textOff="@null"
                    android:textOn="@null" />

                <ru.energyhouse.energyhouse.model.SquareButton
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="24dp"
                    android:layout_weight="1"
                    android:background="@drawable/scenes_click"
                    android:paddingTop="45dp"
                    android:textOff="@null"
                    android:textOn="@null" />


            </LinearLayout>

            <LinearLayout
                android:id="@+id/main_menu_light_secondRow"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ru.energyhouse.energyhouse.model.SquareButton
                    android:id="@+id/main_menu_light_allOff_Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@drawable/scenes_click"
                    android:layout_weight="1"
                    android:text="@null"
                    android:textOff="@null"
                    android:textOn="@null" />

                <ru.energyhouse.energyhouse.model.SquareButton
                    android:id="@+id/main_menu_light_backLight_Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@drawable/scenes_click"
                    android:layout_weight="1"
                    android:text="@null"
                    android:textOff="@null"
                    android:textOn="@null" />

            </LinearLayout>

        </LinearLayout>

    </HorizontalScrollView>

</android.support.constraint.ConstraintLayout>

牛轧糖:enter image description here

棒棒糖:enter image description here

UPD。

如果设置宽度200dp - 它可以工作。我需要像身高和身高一样的宽度 - match_parent。

2 个答案:

答案 0 :(得分:0)

尝试将onMeasure方法更改为以下内容:

select [workers].worker,[combined].name as combined-name,[departments].name as department,[location].name as location  from [workers]
left join [combined] on [workers].combined = [combined].combined
left join [helper] on [helper].ID = [combined].helper
left join [departments] on [departments].ID = [helper].department
left join [location] on [location].ID = [helper].location

答案 1 :(得分:0)

您可以使用带有buttonshape.xml的简单按钮作为Button的背景

<Button
android:text="Button"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/buttonshape"
/>

将此代码粘贴到drawable中buttonshape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" >
<corners
android:radius="20dp"
/>
<solid
android:color="#FFFFF"
/>
<size
android:width="200dp"
android:height="200dp"
/>
</shape>