创建简单的卡片布局 android
Create simple Card layout android
我想创建一个像这张图片一样的简单卡片布局
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="3dp"
android:id="@+id/offer_card">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.3">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/img_offer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
fresco:placeholderImage="@drawable/backgrondlayout2"
fresco:actualImageScaleType="centerCrop" />
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img_logo"
android:layout_margin="10dp"
fresco:placeholderImage="@drawable/ic_launcher"
fresco:actualImageScaleType="centerCrop" />
<TextView
android:text="Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img_logo"
android:layout_alignEnd="@+id/img_logo"
android:id="@+id/txt_name"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@color/white" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="4dp"
android:layout_weight="1">
<TextView
android:text="TitleOfOffer is here sgffs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_Title"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:fontFamily="sans-serif-medium"
android:layout_weight="0.2"
android:textAlignment="textEnd" />
<TextView
android:text="this is a simple discription with losts of text sjdun a;daiwn adwi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_discription"
android:layout_weight="0.2"
android:textAlignment="textEnd" />
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@android:drawable/divider_horizontal_textfield"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:paddingBottom="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<TextView
android:text="Date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_Date"
android:textAlignment="textEnd"
android:drawableRight="@drawable/ic_directions_black_24dp" />
<TextView
android:text="Distance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_distance"
android:textAlignment="textEnd"
android:drawableRight="@drawable/ic_settings_black_24dp"
android:hapticFeedbackEnabled="false"
android:layoutDirection="rtl" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
这是结果
你可以看到左图高度依赖于右图。我怎样才能改革这个布局并使其稳定,因为通过改变文本图像会移动,我还添加了一张图像(img_logo
),它没有出现在 all.Can 你帮我找一个改革我的布局的好解决方案?
谢谢
要保持您的布局比例,您可以在 TextView 上使用此 XML 属性 android:ellipsize
和 android:maxLines
。
TextView 示例:
<TextView android:id="@+id/txt_discription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:ellipsize="end"
android:maxLines="2"
tools:text="This is a simple discription with losts of text lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
/>
这是使用之前的 TextView 将 CardView 转换为 XML 布局文件的简单示例:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp"
>
<ImageView
android:id="@+id/card_background_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
tools:src="@android:mipmap/sym_def_app_icon"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:drawable/screen_background_dark_transparent"
android:layout_gravity="bottom"
android:padding="16dp"
>
<TextView
android:id="@+id/card_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:ellipsize="end"
android:maxLines="1"
tools:text="Cart title"
/>
<TextView android:id="@+id/txt_discription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:ellipsize="end"
android:maxLines="2"
tools:text="this is a simple discription with losts of text lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
/>
</LinearLayout>
</android.support.v7.widget.CardView>
希望对您有所帮助。
我做了一个ListView
和CardView
的小例子。 https://github.com/AndroidAppNotes/ListView-and-CardView
我想创建一个像这张图片一样的简单卡片布局
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="3dp"
android:id="@+id/offer_card">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.3">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/img_offer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
fresco:placeholderImage="@drawable/backgrondlayout2"
fresco:actualImageScaleType="centerCrop" />
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img_logo"
android:layout_margin="10dp"
fresco:placeholderImage="@drawable/ic_launcher"
fresco:actualImageScaleType="centerCrop" />
<TextView
android:text="Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img_logo"
android:layout_alignEnd="@+id/img_logo"
android:id="@+id/txt_name"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@color/white" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="4dp"
android:layout_weight="1">
<TextView
android:text="TitleOfOffer is here sgffs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_Title"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:fontFamily="sans-serif-medium"
android:layout_weight="0.2"
android:textAlignment="textEnd" />
<TextView
android:text="this is a simple discription with losts of text sjdun a;daiwn adwi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_discription"
android:layout_weight="0.2"
android:textAlignment="textEnd" />
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@android:drawable/divider_horizontal_textfield"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:paddingBottom="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<TextView
android:text="Date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_Date"
android:textAlignment="textEnd"
android:drawableRight="@drawable/ic_directions_black_24dp" />
<TextView
android:text="Distance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_distance"
android:textAlignment="textEnd"
android:drawableRight="@drawable/ic_settings_black_24dp"
android:hapticFeedbackEnabled="false"
android:layoutDirection="rtl" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
这是结果
你可以看到左图高度依赖于右图。我怎样才能改革这个布局并使其稳定,因为通过改变文本图像会移动,我还添加了一张图像(img_logo
),它没有出现在 all.Can 你帮我找一个改革我的布局的好解决方案?
谢谢
要保持您的布局比例,您可以在 TextView 上使用此 XML 属性 android:ellipsize
和 android:maxLines
。
TextView 示例:
<TextView android:id="@+id/txt_discription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:ellipsize="end"
android:maxLines="2"
tools:text="This is a simple discription with losts of text lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
/>
这是使用之前的 TextView 将 CardView 转换为 XML 布局文件的简单示例:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp"
>
<ImageView
android:id="@+id/card_background_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
tools:src="@android:mipmap/sym_def_app_icon"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:drawable/screen_background_dark_transparent"
android:layout_gravity="bottom"
android:padding="16dp"
>
<TextView
android:id="@+id/card_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:ellipsize="end"
android:maxLines="1"
tools:text="Cart title"
/>
<TextView android:id="@+id/txt_discription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:ellipsize="end"
android:maxLines="2"
tools:text="this is a simple discription with losts of text lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
/>
</LinearLayout>
</android.support.v7.widget.CardView>
希望对您有所帮助。
我做了一个ListView
和CardView
的小例子。 https://github.com/AndroidAppNotes/ListView-and-CardView