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

发布于 2024-10-21 01:52:36 字数 867 浏览 4 评论 0原文

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

play.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

I have the below view that I need to create. I have the following XML, but the text does not show up and the height fills the entire parent instead of wrapping the content? Any help on creating the below screenshot would be appreciated.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

电影里的梦 2024-10-28 01:52:36

首先,您必须为圆角边缘创建一个 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"/> 

First you have to create a one xml file for rounded edges, then create a one linear layout and set that rounded edges in that back ground and then dynamically add text view to that particular linear layout .

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

Here i attached my rounded edge xml file

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

半夏半凉 2024-10-28 01:52:36

为了更好地设计视图,请为每个视图采用 3 个单独的形状,以便更轻松地设计 UI 并对齐每个形状的文本。

For a better design of your views, take 3 individual shapes for each view so that it is easier to design the UI and align the text for each shape.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文