【问题标题】:How do I add a colour to the top of my android cardviews, like in image?如何在我的 android cardviews 顶部添加颜色,如图像中?
【发布时间】:2015-01-15 12:19:51
【问题描述】:

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

(到目前为止,我的代码没有完成上述操作,到目前为止,我的代码只生成了常规的全白卡片视图)

到目前为止我的代码如下:

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);
        }
    }
}

MainActivity.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);
    }
}

Activity_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>

item_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>

我对你所做的唯一更改是 card_view:cardCornerRadius="8dp"> 并删除了 imageview(不再需要)

抽认卡没有填到一半的屏幕截图:

【问题讨论】:

  • 如何将 22 分钟设置为 accona
  • 我不知道如何,这是我的问题的一部分。我想知道如何在卡片的顶部设置颜色和标题(如果不清楚,我很抱歉)

标签: java android android-layout android-activity android-fragments


【解决方案1】:

我相信这(几乎)是您想要的精确布局。这很不言自明,但如果有不清楚的地方,请随时询问。

<?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,以减少并发症。

更新:问题现在解决了臭名昭著的“圆角”问题,这实际上是设计使然。是的,这是一个很大的缺陷。但是解决方案(如文档 Here 中所给出)将是使用 card_view:cardPreventCornerOverlap="false" 属性(我认为这没有什么好处,因为它只会再次使卡片变成正方形)。

请参阅以下问题以获得对此问题的良好参考:

Appcompat CardView and Picasso no rounded Corners
Make ImageView fit width of CardView

【讨论】:

  • 图片中显示的确切颜色是#109D58。但是,请使用这些调色板来帮助您为应用选择颜色google.com/design/spec/style/color.html#color-color-palette
  • 我注意到绿色矩形 (heading_layout) 留下了空白。而不是填写卡片的上半部分。我该如何解决?
  • 什么空白?您可以发布图片或编辑您的问题吗?您是否使用了我发布的确切布局?
  • 对,一秒钟,我在上面加图
  • 我已经编辑了答案。主要是在platform 21之前无法全效使用cardView。如果你必须需要圆角和适当的颜色背景,使用你自己的自定义视图。反正我不是 cardView 的粉丝(因为圆角和填充问题)
【解决方案2】:

据我了解,您可以更改 CardView 的全部颜色,但不能更改部分颜色。我不确定那会如何工作。

您可以做的是在CardView 中嵌套一个带有标题的TextView,然后将TextView 的背景着色为您想要的颜色。使用适当的边距/填充以获得统一的外观。为您的 TextView 添加背景,看看您会得到什么。

您的 TextView 已经在您的 android:layout_width="" 上使用了 match_parent 参数,因此您只需像这样添加背景:

<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);

【讨论】:

  • 您的 TextView 提案不适用于 Android 19 及更低版本。 TextView 的背景与cardview 不匹配,TextView 周围仍有白色。只有 setCardBackgroundColor si 才有权为所有卡片视图着色
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 2019-06-28
  • 2017-11-22
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
相关资源
最近更新 更多