2017-07-07 60 views
1

我做了一个自定义按钮类来制作方形按钮。按钮在牛轧糖中运行良好,但不会在棒棒糖和棉花糖中显示。我的代码有什么问题?不同API中的自定义按钮

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。

回答

0

尝试方法改变onMeasure到以下几点:

@Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {   
     int size = widthMeasureSpec > heightMeasureSpec ? heightMeasureSpec : widthMeasureSpec; 
     super.onMeasure(size, size); 

    } 
+0

什么都没有发生( – Ivan

0

你可以使用简单的按钮与buttonshape.xml作为按钮

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

的背景中可绘制这段代码粘贴为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> 
+0

我需要使用我的背景, match_parent的高度和宽度,如高度(平方)。 – Ivan

+0

那么你应该使用而不是,因为ImageView同时使用'background'和'src',你可以在其中使用'background'作为square,即'buttonshape.xml'和'src'作为你想要使用的背景或相反亦然 –