android - 如何在 Android View 中的圆角矩形内插入文本?

标签 android xml android-layout shapes

我需要创建以下 View 。我有以下 XML,但文本没有显示,高度填充整个父级而不是包装内容?任何有关创建以下屏幕截图的帮助将不胜感激。

播放.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

最佳答案

首先,您必须为圆形边缘创建一个 xml 文件,然后创建一个线性布局并在该背景中设置该圆形边缘,然后动态地将 TextView 添加到该特定线性布局。

    <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"/> 

关于android - 如何在 Android View 中的圆角矩形内插入文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5228783/

相关文章:

android - RelativeLayout 的旋转如何在 android 中工作?

android - 打开另一个 Activity 时状态栏闪烁

android - 找不到所有蓝牙设备android

android - 屏幕上的用户不活动

java - 将 nu.XOM.Element 转换为 org.w3c.dom.Element

java.sql.BatchUpdateException : Table doesn't exist

excel - Xpath解释

java - map View 上的按钮

php - 如何从 Android 应用程序将数据插入 mysql 数据库?

java - RecyclerView 通过 Intent extras 传递数据