如何在android cardviews的顶部添加颜色,如在图像中?

mv1qrgav  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(392)

我编写了一个小应用程序,其中显示了几个android卡。但我想能够设置一个颜色和标题的卡片顶部如下图所示,到目前为止,我还没有找到任何信息网上如何做到这一点。所以一些帮助将是美妙的:-)

(到目前为止,我的代码还没有实现上述功能,到目前为止,我的代码只生成常规的全白色cardwiews)
我的代码如下:
cardadapter.java文件

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

    public List<TTItem> posts = new ArrayList<>();

    public void addItems(List<TTItem> items) {
        posts.addAll(items);
        notifyDataSetChanged();
    }

    public void clear() {
        posts.clear();
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = View.inflate(context, R.layout.item_cardview, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(posts.get(position).title);
        Picasso.with(holder.mImageView.getContext()).load(posts.get(position).images[0]).into(holder.mImageView);
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ImageView mImageView;

        public ViewHolder(View view) {
            super(view);
            mTextView = (TextView) view.findViewById(R.id.textview);
            mImageView = (ImageView) view.findViewById(R.id.imageView);
        }
    }
}

主活动.java

public class MainActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener {
    @InjectView(R.id.mainView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.refreshContainer)
    SwipeRefreshLayout refreshLayout;
    private LinearLayoutManager mLayoutManager;
    private CardAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        mRecyclerView.setHasFixedSize(true);
        refreshLayout.setOnRefreshListener(this);
        TypedValue tv = new TypedValue();
        int actionBarHeight = 0;
        if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
        }
        refreshLayout.setProgressViewEndTarget(true, actionBarHeight);
        mAdapter = new CardAdapter();
        mRecyclerView.setAdapter(mAdapter);
        // use a linear layout manager
        mLayoutManager = new GridLayoutManager(this, 1);
        mRecyclerView.setLayoutManager(mLayoutManager);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onRefresh() {
        mAdapter.clear();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                refreshLayout.setRefreshing(false);
            }
        }, 2500);
    }
}

活动\u main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refreshContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/mainView"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

项目\u cardview.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:padding="10dp"
    card_view:cardCornerRadius="2dp"
    card_view:contentPadding= "5dp"
    card_view:cardUseCompatPadding="true"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:scaleType="centerCrop"/>
        <TextView
            android:id="@+id/textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_gravity="center"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

我对你唯一的改变就是,卡德_view:cardcornerradius=“8dp”>并删除了imageview(不再需要时)
抽认卡未填充到卡的一半的截图:

gr8qqesn

gr8qqesn1#

我相信这(几乎)就是你想要的布局。这是相当不言自明的,但如果有什么不清楚,请随时询问。

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    android:layout_gravity="center"
    app:cardElevation="8dp"
    card_view:cardCornerRadius="2dp">

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

        <LinearLayout     <!-- This is the specific part you asked to color -->
            android:id="@+id/heading_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/teal_500"
            android:padding="36dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_heading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="22 mins to Ancona"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:textSize="36sp" />

            <TextView
                android:id="@+id/tv_subheading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:layout_below="@+id/tv_heading"
                android:text="Light traffic on ss16"
                android:textColor="@color/teal_200"
                android:textSize="24sp" />

        </LinearLayout>

        <ImageView
            android:id="@+id/iv_map"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="Assigned delivery boy"
            android:scaleType="fitXY"
            android:src="@drawable/bg_map" />

        <TextView
            android:id="@+id/tv_footer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:layout_below="@+id/tv_heading"
            android:text="It is just an example!"
            android:textColor="@color/grey_500"
            android:textStyle="bold"
            android:textSize="24sp" />

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

我已经换了你的 mapFragment (大概)用 imageView 减少并发症。
更新:现在的问题解决了臭名昭著的“拐弯”问题,这实际上是设计的。是的,这是一个很大的缺陷。但解决方案(如本文文档所示)是 card_view:cardPreventCornerOverlap="false" 属性(我不认为它有什么好处,因为它只是使卡再次正方形)。
有关此问题的详细信息,请参见以下问题:
appcompat cardview和毕加索没有圆角
使imageview适合cardview的宽度

px9o7tmv

px9o7tmv2#

据我所知,你可以改变一件衣服的颜色 CardView 它是完整的,但不是部分的。我甚至不知道这是怎么回事。
你能做的,就是筑巢 TextView 标题在 CardView ,然后给背景上色 TextView 你喜欢的颜色。使用适当的边距/填充以获得统一的外观。给你的照片添加背景 TextView 看看你能得到什么。
您的textview已在使用 match_paren 你的t参数 android:layout_width="" 因此,只需添加如下背景:

<TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:background="#FF4444"/>

要更改整个cardview的颜色,如我在开头提到的,您可以使用相同的方法或编程方式进行更改,如下所示:

cardView.setCardBackgroundColor(COLOURHERE);

相关问题