如何在Android视图中的圆角矩形内插入文字?[英] How do I insert text inside a rounded rectangle in and Android view?

本文是小编为大家收集整理的关于如何在Android视图中的圆角矩形内插入文字?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有以下视图,我需要创建.我有以下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>

查看

推荐答案

首先,您必须为舍入边区创建一个XML文件,然后创建一个线性布局,然后将该舍入边区设置为返回地,然后动态地将文本视图添加到该特定的线性布局.

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/Your runded 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"/> 

本文地址:https://www.itbaoku.cn/post/102438.html

问题描述

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

推荐答案

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