2016-02-12 32 views
8

Ich bin neu bei Android und das ist meine erste Frage hier.Wie füge ich einen farbigen Rahmen in Cardview hinzu?

Ich versuche, einen farbigen vertikalen Rahmen am Anfang der Kartenansicht hinzuzufügen. Wie kann ich es auf XML erreichen? Ich habe versucht, es mit leeren Textansicht hinzuzufügen, aber es ist die ganze Kartenansicht selbst versauen. Bitte sehen Sie sich zum Beispiel den unten angegebenen Bildlink an.

Colored Border on Cardview

activity_main.xml

<android.support.v7.widget.CardView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    card_view:contentPadding="16dp" 
    card_view:cardElevation="2dp" 
    card_view:cardCornerRadius="5dp"> 

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

     <TextView 
      style="@style/Base.TextAppearance.AppCompat.Headline" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Title" /> 

     <TextView 
      style="@style/Base.TextAppearance.AppCompat.Body1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Content here" /> 

    </LinearLayout> 

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

Vielen Dank

+0

brauchst du hier ein cardview? Wie wäre es mit einem linearen Planlayout und Hinzufügen einer ItemDecoration zu Ihrer Recycleransicht? –

+0

Ich bin ein Anfänger auf Android und Recyclerview ist etwas fortgeschritten für meine aktuellen Fähigkeiten. Also, ich benutze Cardview hier. –

+0

okay, ich habe eine Lösung für Sie unter –

Antwort

15

versuchen zu tun:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    card_view:cardElevation="2dp" 
    card_view:cardCornerRadius="5dp"> 

    <FrameLayout 
     android:background="#FF0000" 
     android:layout_width="4dp" 
     android:layout_height="match_parent"/> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="16dp" 
     android:orientation="vertical"> 

     <TextView 
      style="@style/Base.TextAppearance.AppCompat.Headline" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Title" /> 

     <TextView 
      style="@style/Base.TextAppearance.AppCompat.Body1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Content here" /> 

    </LinearLayout> 

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

dies die Polsterung aus dem cardview entfernt und fügt eine FrameLayout mit einem Spalte oder. Sie müssen dann die Polsterung in der Linearlayout dann für die anderen Felder

aktualisieren

beheben Wenn Sie die Karte Eckenradius erstellen card_edge.xml in ziehbar Ordner beibehalten möchten:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <solid android:color="#F00" /> 
    <size android:width="10dp"/> 
    <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/> 
    <corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp" 
     android:topRightRadius="0.1dp" android:bottomRightRadius="0.1dp"/> 
</shape> 

und im Frame-Layout verwenden android:background="@drawable/card_edge"

+0

@GeorgeForster aktualisiert, um einen ziehenden Hintergrund zu verwenden, um die Kartenecken zu bewahren, wenn Sie sie immer noch haben wollen –

+0

hat nicht funktioniert für mich. Warum 4 dpi Breite für das Rahmenlayout? –

13

Ich denke, dass diese Lösung nicht effizient sein kann, aber es dient dem Zweck und fügt Flexibilität mit der Grenze Breite.

<android.support.v7.widget.CardView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="40dp" 
    android:layout_gravity="center" 
    card_view:cardBackgroundColor="@color/some_color" 
    card_view:cardCornerRadius="20dp" 
    card_view:contentPadding="5dp"> <!-- Change it to customize the border width --> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     card_view:cardCornerRadius="20dp" 
     card_view:contentPadding="5dp"> 

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

     <!-- Add your UI elements --> 

     </RelativeLayout> 
    </android.support.v7.widget.CardView> 
</android.support.v7.widget.CardView> 
+4

Dies ist eine schlechte Lösung. Sie fügen die Ansichtshierarchieebene hinzu, die zu einer Leistungseinbuße führen würde. – Sadegh

+0

Ja einverstanden, es war nur eine faule Lösung von mir. – Amit

+0

Gute Idee es sollte innere Funktionen zur Verfügung stehen – Sam

Verwandte Themen