分享

如何在android中制作角半径

 印度阿三17 2019-08-23

我正在尝试制作一个gridview,它下面会有一个带有角半径效果的图像和文本.到目前为止似乎不可能.这是我的Activity xml,它持有gridview:

<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./apk/res/android"
              xmlns:app="http://schemas./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./apk/res/android">
    <item>
        <shape xmlns:android="http://schemas./apk/res/android" >
            <solid android:color="#C0C0C0"></solid>
            <corners android:radius="15dp"></corners>
        </shape>
    </item>
</selector>

输出如下:

enter image description here

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

解决方法:

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

将编译’com.android.support:cardview-v7:24.2.0’添加到您的应用程序gradle文件中.

<android.support.v7.widget.CardView xmlns:android="https://schemas./apk/res/android"
xmlns:card_view="https://schemas./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./apk/res/android"
          xmlns:app="http://schemas./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>
来源:https://www./content-4-403251.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约