Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 回收视图切割CardView的侧面_Android_Android Recyclerview - Fatal编程技术网

Android 回收视图切割CardView的侧面

Android 回收视图切割CardView的侧面,android,android-recyclerview,Android,Android Recyclerview,我有一个RecyclerView,可以通过编程方式为CardView充气。卡的左侧被切断,它们没有居中。如果你需要更多的代码张贴,我会很高兴这样做 下面是一些有用的代码: 活动: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.

我有一个RecyclerView,可以通过编程方式为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:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".Activity.ClassRoster"
    tools:showIn="@layout/activity_class_roster"
    android:orientation="vertical"
    android:gravity="center">

<android.support.v7.widget.RecyclerView android:id="@+id/roster_recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:paddingTop="16dp"
    android:clipToPadding="false"
    android:scrollbars="vertical"
    android:gravity="center"/>

</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:id="@+id/student_card_linlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/student_card"
    android:layout_width="@dimen/student_card_width"
    android:layout_height="@dimen/student_card_height"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardPreventCornerOverlap="false"
    android:clickable="true"
    android:foreground="@drawable/custom_bg"
    card_view:cardCornerRadius="@dimen/student_card_radius"
    card_view:cardElevation="@dimen/student_card_elevation">

    <RelativeLayout android:id="@+id/card_layout"
        android:background="@color/a"
        android:layout_width="match_parent"
        android:layout_height="160dp">

        <TextView android:id="@+id/student_name"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:textColor="@android:color/white"
            android:text="TS"
            android:textSize="@dimen/student_card_text_size"
            android:gravity="center"
            android:textIsSelectable="false"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>

    <android.support.v7.widget.Toolbar android:id="@+id/card_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="bottom">

        <ImageView android:id="@+id/student_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="25dp"
            android:layout_marginEnd="5dp"
            android:src="@drawable/ic_delete_black_24dp"/>

        <ImageView android:id="@+id/student_absent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:layout_marginBottom="25dp"
            android:src="@drawable/ic_change_history_black_24dp"/>

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

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

</LinearLayout>

若要使文本居中,请更改
TextView
宽度以匹配父视图

    <RelativeLayout android:id="@+id/card_layout"
    >

       <TextView android:id="@+id/student_name"
            android:layout_width="match_parent" // before it is wrap_content
            ... />

    </RelativeLayout>

使你的底部中心正确

// I organize the flow of your layout from left-to-right
// Before it is right-to-left

<android.support.v7.widget.Toolbar android:id="@+id/card_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="bottom">

       <ImageView android:id="@+id/student_absent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:layout_marginBottom="25dp"
            android:src="@drawable/ic_change_history_black_24dp"/>  

      <ImageView android:id="@+id/student_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="25dp"
            android:src="@drawable/ic_delete_black_24dp"/>

    </android.support.v7.widget.Toolbar>
//我从左到右组织布局的流程
//在它从右向左之前
希望这有帮助