Displaying UI widgets in Cardview in android L
From the description of CardView
CardView extends the FrameLayout class and lets you show information inside cards that have a consistent look on any app. CardView widgets can have shadows and rounded corners.
As you probably know, in FrameLayout
there are no constraints or relations between views positioning. They will be displayed one above each other. If you want to use different hierarchy inside a CardView
please just use any layout you want inside. For example:
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:background="@color/white"
android:layout_width="200dp"
android:layout_height="200dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Textview one"
android:gravity="center"
android:textSize="36sp" />
<TextView
android:id="@+id/info_text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Textview two"
android:gravity="center"
android:textSize="36sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
Treat the CardView
only as an outer decoration and you can use ANY layout inside. So if LinearLayout
is not appropriate you can use RelativeLayout
etc.
Comments
-
Psypher almost 2 years
I am trying to create an application in cardview, the cardview contains two textviews which needs to be placed next to each other, but the textviews are getting overlapped. Is there any mechanism to place elements like its done in RelativeLayout(layout_below or layout_toRightOf) for Cardviews. I am able to do this by adding shadows to the Relativelayout and making it look like Cardview, I want to do this in the Cardview.
My Layout code is below :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="@android:color/black" android:layout_height="match_parent"> <android.support.v7.widget.CardView android:id="@+id/card_view" android:background="@color/white" android:layout_width="200dp" android:layout_height="200dp"> <TextView android:id="@+id/info_text" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Textview one" android:gravity="center" android:textSize="36sp" /> <TextView android:id="@+id/info_text1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Textview two" android:gravity="center" android:textSize="36sp" /> </android.support.v7.widget.CardView> </RelativeLayout>
Currently it is displayed like the picture attached:
-
Psypher over 9 yearsAs I mentioned in my question, I am able to do so by having the contents of the cardview embedded in a layout. I was looking for options not to do so as they are expensive.
-
Maciej Ciemięga over 9 yearsYou wrote: " I am able to do this by adding shadows to the Relativelayout and making it look like Cardview" - it basically means that you are able to "mimic" the
CardView
using aRelativeLayout
and adding a shadow to it, not that you know you can addRelativeLayout
inside of it:) But OK. So you have two solutions: 1. Use inner layout as I suggested in my answer or 2. Extend theCardView
and implement your custom measure and layout methods on your own (the same way as you would create a custom layout and position children from code). There is no other way to do it. -
mDroidd almost 8 yearsAFAIK you cannot extend CardView.
-
Maciej Ciemięga almost 8 yearsWhy would you not be able to do that?