android - android中的边距和布局

标签 android xml layout

我想创建卡片 -

pictures ,

我尝试使用 margin,但没有用这个想法取胜。我尝试为所有元素创建个人 View 并使用 paddingmargin 但我没有结果。

我有这样的代码

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/rlCardBlock"
        style="@style/Widget.CardContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:maxLines="2"
            android:textStyle="bold"
            android:text="title"/>

        <TextView
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:maxLines="5"
            android:text="content" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@id/content"
            android:layout_marginBottom="8dp"
            android:background="@null"
            android:src="@drawable/ic_more_vert_black_36dp" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

感谢您的关注。

最佳答案

Android 已经为卡片和回收站 View 预定义了小部件。您可以使用 android cardview 和 recycler view 来解决您的问题,它很容易实现。

为此 如果你使用 android studio 作为 IDE,你首先添加依赖项

compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'

然后在上面提到的代码中将其更改为

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:padding="16dp"
    >

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cv"
        >



<LinearLayout
android:id="@+id/rlCardBlock"
style="@style/Widget.CardContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:maxLines="2"
    android:textStyle="bold"
    android:text="title"/>

<TextView
    android:id="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:layout_marginTop="12dp"
    android:maxLines="5"
    android:text="content" />

 <TextView
    android:id="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/description"
    android:layout_marginTop="16dp"
    android:maxLines="5"
    android:text="content" />

<LinearLayout
android:id="@+id/rlCardBlock"
style="@style/Widget.CardContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal">

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Action 1"
  android:id="@+id/button1"
  />

 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Action 2"
  android:id="@+id/button2"
  />

</LinearLayout>

</LinearLayout>

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

</LinearLayout>

这是教程:link

关于android - android中的边距和布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36239648/

相关文章:

php - 从 RSS 提要中删除图像

android - 如何制作内联文本编辑器和 ImageView

java - 根据 XSD 验证 XML 顺序的框架

android - RecyclerView match_parent 中带有 horizo​​ntalScrollview 的自定义 View 不起作用

android - 如何赋予 Android 3.2 与 Android 2.2 应用程序相同的外观?

android - Android 中更快的 GPS 定位

android - 线性布局 : how to place a view below/right of a fill_parent view?

android - 我的布局文件中的红色感叹号是什么意思?

android - 如何将 Android DataBinding 绑定(bind)到菜单?

java - 水平向JPanel添加一个又一个组件