我做了一個自定義按鈕類來製作方形按鈕。按鈕在牛軋糖中運行良好,但不會在棒棒糖和棉花糖中顯示。我的代碼有什麼問題?不同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。
什麼都沒有發生( – Ivan