【问题标题】:Add cardview programmatically from xml从 xml 以编程方式添加 cardview
【发布时间】:2017-01-17 08:46:41
【问题描述】:

我正在编写一个 Android 应用程序,并有一个从列表中获取的 cmets 列表,但我想动态添加它,方法是将其写入编辑文本并使用按钮发送。是否可以添加一个新的 cardView 并输入我在编辑文本中写入的数据? 任何帮助都将不胜感激。谢谢。

这是我的活动代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/linearLayout">

    <LinearLayout
        android:id="@+id/top_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#ffffff"
        android:orientation="horizontal"
        android:gravity="center_horizontal">

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

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="volver"
                android:id="@+id/backBut"
                android:backgroundTint="#ffffff" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="COMENTARIOS"
                android:id="@+id/ventas"
                android:gravity="center_vertical"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                android:textStyle="bold|italic"
                android:typeface="sans"
                android:textColor="#000000" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/refreshbutton"
                android:layout_marginRight="10dp"
                android:background="@drawable/reload"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />


        </RelativeLayout>
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/reciclador"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_bar"
        android:padding="3dp"
        android:scrollbars="vertical"
        android:layout_marginBottom="50dp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/commentlayout">

        <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#ffffff"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">
            <EditText
                android:id="@+id/commenttext"
                android:hint="Escribe tu comentario"
                android:layout_weight="0.9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            />

            <ImageButton
                android:layout_weight="0.1"
                android:id="@+id/sendButton"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_toRightOf="@id/commenttext"
                android:background="@drawable/send" />
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>

为了刷新我的数据,我使用它:

void refresh(List<Comment> commentsList){
        mAdapter = new CommentsAdapter(commentsList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
}

但这不是自动的。我想要facebook之类的东西。

谢谢。

【问题讨论】:

  • 请分享一些你所拥有的代码。
  • 你想在cardview中显示像facebook这样的列表吗?

标签: android xml android-studio android-cardview cardview


【解决方案1】:
 <?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:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="2dp"
    card_view:contentPadding="10dp">

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

        <EditText
            android:id="@+id/editext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_alignParentTop="true"/>

        <EditText
            android:id="@+id/edittext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/edittext"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

或在 Gradle 中添加

compile 'com.android.support:cardview-v7:22.0.0'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2017-07-12
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多