无法检索卡片视图中的LinearLayout

时间:2016-12-24 20:21:27

标签: android android-linearlayout android-cardview viewgroup

我正在使用ListView BaseAdapter。 仅在平板电脑中,我无法在LinearLayout的{​​{1}}方法中查看CardView(位于getView内)的视图。

BaseAdapter

它总是给if(convertView == null) { convertView = activity.getLayoutInflater().inflate(layout, null); } ((TextView)convertView.findViewById(R.id.title)).setText(post.getTitle(); View view = convertView.findViewById(R.id.child_view); if (view != null) Log.e("ssssssss","view != null"); else { Log.e("ssssssss","view == nullllllllllllllllll"); } 。我可以null。但是,我无法得到R.id.card_view& R.id.child_view

R.id.inner_child_view
我正在使用的

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.CardView android:id="@+id/card_view" style="@style/CardViewNormal" card_view:cardBackgroundColor="@android:color/white" card_view:cardPreventCornerOverlap="false" card_view:cardUseCompatPadding="true" card_view:cardElevation="4dp" card_view:cardCornerRadius="2dp"> <LinearLayout android:id="@+id/child_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:id="@+id/inner_child_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:orientation="horizontal"> <ImageView android:id="@+id/calendar_icon" android:layout_width="12dp" android:layout_height="12dp" android:layout_marginRight="3dp" android:tint="#aaa" android:layout_gravity="center_vertical" android:src="@drawable/ic_event_black_18dp"/> <TextView android:id="@+id/date" android:layout_width="wrap_content" android:textStyle="bold" android:textColor="#aaa" android:text="2015-09-25" android:textSize="12sp" android:layout_toRightOf="@id/calendar_icon" android:layout_gravity="center_vertical" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:paddingRight="10dp" android:textSize="16sp" android:textColor="@color/black" android:maxLines="2" android:ellipsize="end" android:layout_marginBottom="10dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:text="Expected Questions: Seating Arrangements 1234 1234567890123456123465789123456789" android:layout_height="wrap_content"/> <TextView android:id="@+id/category" android:text="Alerts" android:paddingLeft="5dp" android:paddingRight="5dp" android:paddingTop="2dp" android:paddingBottom="2dp" android:textSize="12sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout> 版本是CardView

1 个答案:

答案 0 :(得分:1)

您可以在此处查看结果:enter image description here

我测试了你的代码: 修改了一下(不多) 但它也适用于平板电脑。 响应是:E / ssssssss:view!= null

我用来测试的代码(修改过的代码):

    Log.e("ssssssss","inside oncreate");
    View view = findViewById(R.id.child_view);
    if (view != null)
        Log.e("ssssssss","view != null");
    else {
        Log.e("ssssssss","view == nullllllllllllllllll");
    }

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        card_view:cardBackgroundColor="@android:color/white"
        card_view:cardPreventCornerOverlap="false"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="4dp"
        card_view:cardCornerRadius="2dp"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <LinearLayout
        android:id="@+id/child_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/inner_child_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/calendar_icon"
                android:layout_width="12dp"
                android:layout_height="12dp"
                android:layout_marginRight="3dp"
                android:tint="#aaa"
                android:layout_gravity="center_vertical"
                android:src="@drawable/ic_cast_dark"/>

            <TextView
                android:id="@+id/date"
                android:layout_width="wrap_content"
                android:textStyle="bold"
                android:textColor="#aaa"
                android:text="2015-09-25"
                android:textSize="12sp"
                android:layout_toRightOf="@id/calendar_icon"
                android:layout_gravity="center_vertical"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:paddingRight="10dp"
            android:textSize="16sp"
            android:textColor="@color/cardview_dark_background"
            android:maxLines="2"
            android:ellipsize="end"
            android:layout_marginBottom="10dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:text="Expected Questions: Seating Arrangements 1234 1234567890123456123465789123456789"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/category"
            android:text="Alerts"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="2dp"
            android:paddingBottom="2dp"
            android:textSize="12sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>