如何在 CardView 中执行 setVisibility?我想为 CardView 做 setVisibility (GONE)。我不想做LinearLayout setVisibility(GONE)。编程。怎么做?
<android.support.v7.widget.CardView
android:id="@+id/cv_ingredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/cardMarginVertical"
android:layout_marginLeft="@dimen/cardMarginHorizontal"
android:layout_marginRight="@dimen/cardMarginHorizontal"
android:layout_marginTop="@dimen/cardMarginVertical"
app:cardBackgroundColor="@color/colorWhite"
app:cardCornerRadius="2dp"
app:cardElevation="5dp"
app:cardPreventCornerOverlap="false"
app:contentPadding="0dp">
<LinearLayout
android:id="@+id/linear_ingredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ingredients"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ListView
android:id="@+id/list_ingredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v7.widget.CardView>
你为什么不这样做:
CardView cv_ingredient = (CardView) findViewById(R.id.cv_ingredient);
cv_ingredient.setVisibility(View.GONE);
你不能做那个人!,因为CardView
在这里充当LinearLayout
的父母视图。父视图的可见性也会影响子视图。
父视图的可见性设置为"消失",则所有子视图也将消失,无法设置线性布局可见的白色卡视图已消失。我建议重新设计你的layout
尝试将此属性添加到第二个 CardView
android:visibility="gone"
只需在Android studio IDE中单击无效缓存/重新启动。这是一个 IDE 错误。之后,您可以在java文件中导入CardView class
。
import android.support.v7.widget.CardView;
CardView editProfileLayout = (CardView) rootView.findViewById(R.id.cardview_layout);
editProfileLayout.setVisibility(View.GONE);
您可以在 linearLayout 中使用卡片视图:
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/cv_ingredient"
app:cardBackgroundColor="@color/colorWhite"
app:cardCornerRadius="2dp"
app:cardElevation="5dp"
app:cardPreventCornerOverlap="false"
app:contentPadding="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/cardMarginVertical"
android:layout_marginLeft="@dimen/cardMarginHorizontal"
android:layout_marginRight="@dimen/cardMarginHorizontal"
android:layout_marginTop="@dimen/cardMarginVertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ingredients"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ListView
android:id="@+id/list_ingredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
</android.support.v7.widget.CardView>
</LinearLayout>
我在我的应用程序中使用了这样的,这个 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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:id="@+id/ll_customer_details_row"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/gray_drawer">
<TextView
android:id="@+id/cutomerDetails_textView_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textSize="10sp" />
<TextView
android:id="@+id/cutomerDetails_textView_name"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_below="@+id/cutomerDetails_textView_date"
android:paddingLeft="5dp" />
<TextView
android:id="@+id/cutomerDetails_textView_number"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_below="@+id/cutomerDetails_textView_name"
android:layout_marginLeft="5dp" />
<ImageButton
android:id="@+id/callButtonImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/cutomerDetails_textView_name"
android:layout_marginRight="5dp"
android:background="@color/transparent"
android:src="@drawable/phone" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>