如何在Android视图中的圆角矩形内插入文本?

4
我需要创建下面的视图。我有以下XML,但是文本不显示,并且高度占满整个父级而不是包裹内容?如果能帮忙创建下面的截图将不胜感激。 playing.xml
<View 
    android:background="@drawable/rounded_edges"
    android:text="Current Track" 
    android:textColor="#FFFFFF"
    android:id="@+id/current_track"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:editable="false">
</View>

rounded_edges.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <solid android:color="#1F1F1F"/>
  <corners android:radius="5px"/>
  <padding android:left="20dp" android:top="20dp" android:right="20dp" android:bottom="20dp" /> 
</shape>

view

2个回答

3

首先,您需要创建一个用于圆角的xml文件,然后创建一个线性布局并将其设置为该背景中的圆角,然后动态地向该特定线性布局添加文本视图。

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/your_rounded_edges_xml_file"
            android:orientation="vertical"
            android:layout_marginRight="10dp"   
            android:id="@+id/linearLayout">

</LinearLayout>

我在这里附上了我的圆角边缘XML文件

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

<stroke android:width="1dp"
        android:color="#ababab"
        />

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

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


0
为了更好地设计您的视图,请为每个视图使用3个单独的形状,以便更容易地设计UI并对齐每个形状的文本。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接