Android 具有圆角的ListView行

Android 具有圆角的ListView行,android,xml,android-layout,listview,Android,Xml,Android Layout,Listview,我目前正在重写我的一个iOS应用程序,以便在Android上发布。我取得了很好的进展,但我正在寻找一些关于处理列表行的最佳方法的指导 以下是我正在尝试重新创建的单元格/行: 正如你所看到的,我有一个白色背景视图,每个边和圆角周围都有填充物,所以我想为此我需要将所有内容嵌入某种视图中?我不确定的另一部分是如何创建右侧的彩色圆圈。这是另一个圆角和彩色背景的视图吗?我还没有弄清楚如何在我目前拥有的两个文本视图中找到正确的位置。所以,如果有人能给出一个代码示例,那就太好了 这是我当前的XML:

我目前正在重写我的一个iOS应用程序,以便在Android上发布。我取得了很好的进展,但我正在寻找一些关于处理列表行的最佳方法的指导

以下是我正在尝试重新创建的单元格/行:

正如你所看到的,我有一个白色背景视图,每个边和圆角周围都有填充物,所以我想为此我需要将所有内容嵌入某种视图中?我不确定的另一部分是如何创建右侧的彩色圆圈。这是另一个圆角和彩色背景的视图吗?我还没有弄清楚如何在我目前拥有的两个文本视图中找到正确的位置。所以,如果有人能给出一个代码示例,那就太好了

这是我当前的XML:


到目前为止,这似乎对我所拥有的一切都很有效,但我不确定接下来该怎么办。这就是它的样子:


有什么建议吗

对于彩色圆圈-我建议只将文本视图设置为适当大小,并将可绘制的圆圈设置为视图的背景。你应该得到你想要的


对于背景-我只需将整行粘贴在线性或相对布局中,然后将RoundedBitmapDrawable设置为布局的背景。这将为您提供圆角背景效果。如有必要,在每个视图的顶部和底部添加一些边距,以增加项目之间的间距。

对于彩色圆圈,我建议只将文本视图设置为适当大小,并将可绘制的圆圈设置为视图的背景。你应该得到你想要的


对于背景-我只需将整行粘贴在线性或相对布局中,然后将RoundedBitmapDrawable设置为布局的背景。这将为您提供圆角背景效果。如有必要,在每个视图的顶部和底部添加一些边距,以增加项目之间的间距。

我想您正在为listview的项目使用自定义视图,如果是这样,我会将自定义视图的背景设置为如下所示:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/white" />

<stroke android:width="4dp" android:color="@color/beige" /> //If you want a stroke

<corners
    android:topLeftRadius="20dp"
    android:topRightRadius="20dp"
    android:bottomLeftRadius="20dp"
    android:bottomRightRadius="20dp" />

<padding 
    android:left="10dp" 
    android:top="10dp" 
    android:right="10dp"                  
    android:bottom="10dp" />

//如果你想中风

我假设您正在为listview的项目使用自定义视图,如果是这样,我会将自定义视图的背景设置为如下所示:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/white" />

<stroke android:width="4dp" android:color="@color/beige" /> //If you want a stroke

<corners
    android:topLeftRadius="20dp"
    android:topRightRadius="20dp"
    android:bottomLeftRadius="20dp"
    android:bottomRightRadius="20dp" />

<padding 
    android:left="10dp" 
    android:top="10dp" 
    android:right="10dp"                  
    android:bottom="10dp" />

//如果你想中风

作为另一种解决方案,我刚从CardView中得到了一些东西,并使用毕加索对圆进行了圆变换

在这个简单的示例中,我使用了CardView的RecyclerView

首先,这里是毕加索使用的CircletTransformation类:

导入android.graphics.Bitmap;
导入android.graphics.BitmapShader;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.graphics.Paint.Style;
导入com.squareup.picasso.Transformation;
公共类CircletTransform实现转换{
@凌驾
公共位图转换(位图源){
int size=Math.min(source.getWidth(),source.getHeight());
intx=(source.getWidth()-size)/2;
int y=(source.getHeight()-size)/2;
位图squaredBitmap=Bitmap.createBitmap(源、x、y、大小、大小);
if(squaredBitmap!=源){
source.recycle();
}
位图Bitmap=Bitmap.createBitmap(大小,大小,source.getConfig());
画布=新画布(位图);
BitmapShader着色器=新的BitmapShader(squaredBitmap、BitmapShader.TileMode.CLAMP、BitmapShader.TileMode.CLAMP);
Paint Paint=新的Paint(Paint.ANTI_别名_标志| Paint.抖动_标志| Paint.FILTER_位图_标志);
画图。设置阴影(着色器);
浮子r=尺寸/2f;
帆布。画圈(r,r,r-1,油漆);
Paint paintBorder=新的Paint();
paintBorder.setStyle(Style.STROKE);
paintBorder.setColor(Color.argb(84,0,0,0));
paintBorder.setAntiAlias(true);
油漆边框。设置行程宽度(1);
画布。画圈(r,r,r-1,画框);
recycle();
返回位图;
}
@凌驾
公共字符串密钥(){
返回“圈”;
}
}
以下是片段:

公共类BlankFragment扩展了Fragment{
公共空白片段(){
//必需的空公共构造函数
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图根视图=充气机。充气(R.layout.fragment\u blank,container,false);
RecyclerView rv=(RecyclerView)rootView.findViewById(R.id.rv\u recycler\u视图);
rv.setHasFixedSize(真);
MyAdapter=newMyAdapter(新字符串[]{“testone”、“testtwo”、“testthree”、“testfour”},getActivity());
rv.设置适配器(适配器);
LinearLayoutManager llm=新的LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
返回rootView;
}
}
fragment_blank.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
       </android.support.v7.widget.RecyclerView>   
</RelativeLayout>


作为另一种解决方案,我刚从CardView中得到了一些东西,并使用毕加索对圆进行了圆变换

在这个简单的示例中,我使用了CardView的RecyclerView

首先,这里是毕加索使用的CircletTransformation类:

导入android.graphics.Bitmap;
导入android.graphics.BitmapShader;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.graphics.Paint.Style;
导入com.squareup.picasso.Transformation;
公共类CircletTransform实现转换{
@凌驾
公共位图转换(位图源){
int size=Math.min(source.getWidth(),source.getHeight());
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="78dp" >

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        card_view:cardCornerRadius="4dp"
        android:layout_width="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:layout_height="match_parent">

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

        <TextView 
            android:id="@+id/tv_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test" >       
            </TextView>


        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" 
            android:layout_marginRight="10dp"/>

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


</RelativeLayout>