简体   繁体   English

android 列表项的圆角子视图

[英]android list item's child view with rounded corners

I am developing an Alarm Clock application.我正在开发一个闹钟应用程序。 I want an activity to list all the existing alarms.我想要一个活动来列出所有现有的警报。 I am using a custom layout for each row of the ListView.我正在为 ListView 的每一行使用自定义布局。 It should display the alarm's description, type and the time.它应该显示警报的描述、类型和时间。

Here is the row layout: alarm_item_2.xml这是行布局:alarm_item_2.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="@drawable/selector">
    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/time_background"
        android:textColor="#ffffff"
        android:layout_marginTop="6dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:padding="6dp"
        android:textSize="36sp"
    />
    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/time"
        android:paddingTop="6dp"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="#000"
    />
    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/time"
        android:layout_below="@id/description"
        android:textSize="18sp"
        android:textColor="#000"
    />
</RelativeLayout>

Also the layout for the activity: view_alarms.xml还有活动的布局:view_alarms.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          >
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="#fff"
    android:background="#fff"
    android:drawSelectorOnTop="true"
/>
</LinearLayout>

What I want is to make the view displaying the time be black and with round corners.我想要的是使显示时间的视图变为黑色并带有圆角。 I am using a shape resource: time_background_xml in res/drawable folder:我正在使用形状资源: res/drawable 文件夹中的 time_background_xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="#000000"/>
    <corners android:radius="10dp"/>
</shape>

However, the background of the button remains white.但是,按钮的背景仍然是白色。 What is the problem with my code?我的代码有什么问题? Obviously, I'm missing something important.显然,我错过了一些重要的事情。

I think the time_background_xml should be like this:我认为 time_background_xml 应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#000000"/>    
            <corners android:radius="10dp"/>
        </shape>
    </item>    
</layer-list>

Take a look at this for more help.看看这个以获得更多帮助。

UPDATE:更新:

I think this will work:我认为这会起作用:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="#000000"/>    

    <stroke android:width="3dp"
            android:color="#000000"/>

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

    <corners android:radius="10dp"/> 
</shape>

Also, take a look at this for an alternative solution.另外,请查看以获取替代解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM