我无法将OnClicklistener添加到Constraint Layout中。它总是返回null。
我已经定义了一个ComplexButton:
public class ComplexButton{
private TextView title;
private TextView subTitle;
private ImageView icon;
private ConstraintLayout clickableArea;
public ComplexButton(ConstraintLayout layout) {
title = (TextView) layout.findViewById(R.id.textview_title);
subTitle = (TextView) layout.findViewById(R.id.textview_button_subtitle);
icon = (ImageView) layout.findViewById(R.id.imageview_button_icon);
clickableArea = (ConstraintLayout) layout.findViewById(R.id.complex_button_clickable_area_new);
}
我可以设置TextView和ImageView。 但是对于clickableArea,它返回null。
在我的另一节课中,我制作了一个实例来设置元素。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.NotOfInterrest, container, false);
ConstraintLayout complexButtonLayout= (ConstraintLayout) view.findViewById(R.id.buttonHotspotFinder_new);
ComplexButton complexButton= new ComplexButton(complexButtonLayout);
complexButton.setTitle( getString(R.string.tab_find_hotspot) );
complexButton.setSubtitle( "Browse hotspots on our map" );
complexButton.setImageResource( R.drawable.show_hotspots_map );
complexButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), NotOfInterrest.class);
startActivity(intent);
}
});
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="wrap_content"
android:background="@drawable/complex_button_layout"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:id="@+id/complex_button_clickable_area_new">
<ImageView
android:id="@+id/imageview_button_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.027"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/buy_hotspot_subscription" />
<ImageView
android:id="@+id/imageview_right_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_keyboard_arrow_right_black_48dp" />
<TextView
android:id="@+id/textview_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textColor="@color/colorPrimary"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toRightOf="@+id/imageview_button_icon"
app:layout_constraintRight_toLeftOf="@+id/imageview_right_arrow"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textview_button_subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toRightOf="@+id/imageview_button_icon"
app:layout_constraintRight_toLeftOf="@+id/imageview_right_arrow"
app:layout_constraintTop_toBottomOf="@+id/textview_title"
app:layout_constraintVertical_bias="0.0" />
如上所述:我可以设置每个图像和文字。 但是对于clickableArea,我得到空指针。
对于ConstraintLayout,我使用“compile”com.android.support.constraint:constraint-layout:1.0.2'“ 有更新的版本吗?
答案 0 :(得分:2)
您正试图在ConstraintLayout
ConstraintLayout
//Here layout is already an instance of your ConstraintLayout and you are again finding a ConstraintLayout in that
clickableArea = (ConstraintLayout) layout.findViewById(R.id.complex_button_clickable_area_new);
您已经找到了ContraintLayout,并且您将传递给ComplexButton
类的构造函数
ConstraintLayout complexButtonLayout= (ConstraintLayout) view.findViewById(R.id.buttonHotspotFinder_new);
ComplexButton complexButton= new ComplexButton(complexButtonLayout);
改为
public class ComplexButton {
private TextView title;
private TextView subTitle;
private ImageView icon;
private ConstraintLayout clickableArea;
public ComplexButton(ConstraintLayout layout) {
title = (TextView) layout.findViewById(R.id.textview_title);
subTitle = (TextView)
layout.findViewById(R.id.textview_button_subtitle);
icon = (ImageView) layout.findViewById(R.id.imageview_button_icon);
//because layout is your constraint layout
clickableArea = layout;
}
}