簡體   English   中英

在Android中在圓形圖像/矩形內實現TextView的最佳方法

[英]Best way to Implement textview inside circular image/rectangle in android

我正在開發一個Android應用,需要在圓圈內顯示一些文本,例如

在此處輸入圖片說明

文本是動態的。類似地,我想在矩形形狀內實現textview 在此處輸入圖片說明

我發現shape xml文件可用於繪制一些形狀,例如

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <corners android:radius="20dip"/>
    <stroke android:color="@color/colorPrimary" android:width="5dip"/>
    <solid android:color="@android:color/transparent"/>
</shape>

但這是不合適的,因為我想要在文本和圓之間留一些空白。我也嘗試在android:background中使用android:background ,但無法實現上述方案。

實現上述方案的最佳方法是什么? 如何實現這種類似方案? 我可以使用一些自定義圖像來更改圓形/矩形形狀嗎?如何實現?

您可以使用類似這樣的方法。

btn_bg.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="50dp" />
    <solid android:color="#000"/>
</shape>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="Total Count"
        android:layout_marginTop="40dp"
        android:textSize="36dp"
        android:background="@drawable/btn_bg"
        android:textColor="#fff"
        android:id="@+id/btn"
        />

</RelativeLayout>

嘗試這個:-

在您的xml中:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/test">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="11" 
        android:layout_centerInParent="true"/>
</RelativeLayout>

@ drawable / test

<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <corners android:radius="20dip"/>
        <size android:height="40dp" android:width="40dp"/>
        <stroke android:color="@color/colorPrimary" android:width="2dip"/>
        <solid android:color="@android:color/transparent"/>
    </shape>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM