如何在android中生成角半径

如何在android中生成角半径,android,android-layout,gridview,Android,Android Layout,Gridview,我正在尝试制作一个gridview,它下面有一个带有圆角半径效果的图像和文本。到目前为止,这似乎是不可能的。以下是保存gridview的我的活动xml: <ScrollView android:layout_width="match_parent" android:fillViewport="true" android:layout_height="match_parent"> <GridView android:id="@+id

我正在尝试制作一个gridview,它下面有一个带有圆角半径效果的图像和文本。到目前为止,这似乎是不可能的。以下是保存gridview的我的活动xml:

<ScrollView
    android:layout_width="match_parent"
    android:fillViewport="true"
    android:layout_height="match_parent">
    <GridView
        android:id="@+id/gridview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnWidth="140dp"
        android:padding="5dp"
        android:layout_marginTop="10dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="5dp"
        android:horizontalSpacing="5dp"
        android:stretchMode="columnWidth"
        android:gravity="center"/>
</ScrollView>

以下是适配器xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_height="wrap_content"
              android:padding="2dp"
              android:layout_margin="20dp"
              android:background="@drawable/grid_corner_radius"
              android:layout_width="wrap_content">
    <ImageView
        android:layout_width="200dp"
        android:background="@drawable/cat_life"
        android:layout_height="160dp"/>

    <TextView
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:id="@+id/credit_textView"
        android:gravity="center"
        android:text="Life"
        android:textColor="#FFFFFF"
        android:background="@color/colorPrimary"
        android:textSize="15dp"/>
</LinearLayout>    

最后,这里是拐角半径的魔法代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" >
            <solid android:color="#C0C0C0"></solid>
            <corners android:radius="15dp"></corners>
        </shape>
    </item>
</selector>

输出如下所示:


查看图像的锐利矩形边缘是否可见。可能的解决方案是什么?

您可以使用CardView封装适配器xml并将cornerRadius设置为它。它应该可以工作

编译'com.android.support:cardwiew-v7:24.2.0'
添加到你的应用程序升级文件中

<android.support.v7.widget.CardView xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:card_view="https://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_margin="20dp"
card_view:cardCornerRadius="4dp">
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_height="match_parent"
          android:layout_width="match_parent">
<ImageView
    android:layout_width="200dp"
    android:background="@drawable/cat_life"
    android:layout_height="160dp"/>

<TextView
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:id="@+id/credit_textView"
    android:gravity="center"
    android:text="Life"
    android:textColor="#FFFFFF"
    android:background="@color/colorPrimary"
    android:textSize="15dp"/>
      </LinearLayout>    
</android.support.v7.widget.CardView>

尝试以下代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:layout_margin="20dp"
    android:background="@drawable/corners_layout"
    android:layout_width="wrap_content">
    <ImageView
        android:layout_width="200dp"
        android:layout_height="160dp"
        android:background="@drawable/corners_image"/>

    <TextView
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:id="@+id/credit_textView"
        android:gravity="center"
        android:text="Life"
        android:textColor="#FFFFFF"
        android:background="@drawable/corners_text"
        android:textSize="15dp"/>
</LinearLayout>

将此样式用于布局:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimaryJob"/>
    <corners android:radius="20dp"/>
</shape>

将此样式用于textview:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorBlue"/>
    <corners android:bottomLeftRadius="20dp" android:bottomRightRadius="20dp"/>
</shape>

将此样式用于imageview:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimaryJob"/>
    <corners android:topLeftRadius="20dp" android:topRightRadius="20dp"/>
</shape>


您可以使用一些,也可以将xml背景设置为textview。顺便说一句,您可以通过将图像设置为textview的顶部复合绘图表来保存LinearLayout和ImageView。非常适合提高性能(布局层次结构越平坦,活动/片段的速度就越快)。当然这是
xmlns:android=”http://schemas.android.com/apk/res/android“
,根据上面的评论,进入TextView声明。谢谢@Dilpret。找不到获取错误资源。我需要导入什么吗?哦,是的,只需将compile'com.android.support:cardwiew-v7:24.2.0'添加到您的gradle文件中即可。谢谢您的努力@dilpret。似乎不起作用:(。如果我找到任何解决方案,我会更新。