如何在我的Android卡片组的顶部添加颜色,如图像?

时间:2015-01-15 12:19:51

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

我写了一个小应用程序,它显示了几张安卓卡。但我希望能够在卡片的顶部设置颜色和标题,如下图所示,到目前为止,我还没有在网上找到任何信息如何执行此操作。所以一些帮助会非常棒: - )

enter image description here

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

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

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”&gt;并删除了imageview(不再需要)

闪卡无法填充一半卡片的屏幕截图:

enter image description here

2 个答案:

答案 0 :(得分:4)

我相信这是(几乎)精确布局你想要的。这是非常不言自明的,但可以随意询问是否有不清楚的事情。

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

答案 1 :(得分:0)

根据我的理解,您可以更改其中CardView的整体颜色,而不是其中的部分颜色。我不确定这是怎么回事。

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

您的TextView已使用match_paren上的android:layout_width="" t参数,因此您只需添加背景,如下所示:

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