单击文本视图应用程序注释响应

时间:2017-07-01 06:42:13

标签: android textview android-cardview

我正在使用cardView并在textView内部显示此OnClick应用粉碎的数据。textView

.xml文件

        <android.support.v7.widget.CardView
        android:id="@+id/spcard_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/card_margin"
        android:elevation="3dp"
        card_view:cardCornerRadius="@dimen/card_album_radius">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/spthumbnail"
                android:layout_width="match_parent"
                android:layout_height="@dimen/album_cover_height"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                android:scaleType="fitXY"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <TextView
                android:id="@+id/sptitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/album_title_padding"
                android:minLines="3"
                android:paddingRight="@dimen/album_title_padding"
                android:paddingTop="@dimen/album_title_padding"
                android:textColor="@color/album_title"
                android:textSize="@dimen/album_title"
                android:layout_below="@+id/spthumbnail"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <TextView
                android:id="@+id/spcount"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/sptitle"
                android:paddingBottom="@dimen/songs_count_padding_bottom"
                android:minLines="2"
                android:paddingLeft="@dimen/album_title_padding"
                android:paddingRight="@dimen/album_title_padding"
                android:textSize="@dimen/songs_count" />

            <ImageView
                android:id="@+id/spoverflow"
                android:layout_width="@dimen/ic_album_overflow_width"
                android:layout_height="@dimen/ic_album_overflow_height"
                android:layout_alignParentRight="true"
                android:layout_below="@id/spthumbnail"
              android:layout_marginTop="@dimen/ic_album_overflow_margin_top"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_dots" />
         </RelativeLayout>
 <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">
    <Button
        android:id="@+id/spdetailsbuynow"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="buy now"
        android:textColor="#ffffff"
        android:background="@drawable/spdetailsbutton"/>
    <Button
        android:id="@+id/spdetailsaddcart"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="add cart"
        android:textColor="#ffffff"
        android:layout_alignParentRight="true"
        android:background="@color/orange_500"/>
</LinearLayout>
        </LinearLayout>

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

错误日志是

  

java.lang.IllegalStateException:无法在a中找到方法true(View)   父级或祖先用于android的上下文:定义onClick属性   查看类android.widget.LinearLayout

1 个答案:

答案 0 :(得分:-2)

use like this:

<LinearLayout 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:background="@color/off_white_bg"
android:orientation="vertical"
android:layout_height="match_parent">


<android.support.v7.widget.CardView
    android:id="@+id/spcard_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_margin="@dimen/card_margin"
    android:elevation="3dp"
    card_view:cardCornerRadius="@dimen/card_album_radius">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/spthumbnail"
            android:layout_width="match_parent"
            android:layout_height="@dimen/album_cover_height"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:clickable="true"
            android:scaleType="fitXY"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/sptitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/album_title_padding"
            android:minLines="3"
            android:paddingRight="@dimen/album_title_padding"
            android:paddingTop="@dimen/album_title_padding"
            android:textColor="@color/album_title"
            android:textSize="@dimen/album_title"
            android:layout_below="@+id/spthumbnail"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/spcount"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/sptitle"
            android:paddingBottom="@dimen/songs_count_padding_bottom"
            android:minLines="2"
            android:paddingLeft="@dimen/album_title_padding"
            android:paddingRight="@dimen/album_title_padding"
            android:textSize="@dimen/songs_count" />

        <ImageView
            android:id="@+id/spoverflow"
            android:layout_width="@dimen/ic_album_overflow_width"
            android:layout_height="@dimen/ic_album_overflow_height"
            android:layout_alignParentRight="true"
            android:layout_below="@id/spthumbnail"
          android:layout_marginTop="@dimen/ic_album_overflow_margin_top"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_dots" />
     </RelativeLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="2">
<Button
    android:id="@+id/spdetailsbuynow"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="buy now"
    android:textColor="#ffffff"
    android:background="@drawable/spdetailsbutton"/>
<Button
    android:id="@+id/spdetailsaddcart"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="add cart"
    android:textColor="#ffffff"
    android:layout_alignParentRight="true"
    android:background="@color/orange_500"/>
</LinearLayout>
    </LinearLayout>

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

</LinearLayout>