TextView与背景和居中的文本

Moo*_*lit 7 android background textview

我想创建一个具有自定义背景的TextView.特别是我希望有一个看起来像这样的Text视图.我知道我可以设置TextView的背景android:background="@drawable/displaybackground但是我如何在这个textview中对齐文本.文本应显示在Textview的中心,如上图所示.

亲切的问候

M.S*_*eer 26

使用centergravity在TextView的(而不是属性layout_gravity).


小智 6

给你。您只需要找到您喜欢的背景即可。

<Textview
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:gravity="center_vertical|center_horizontal" //center the text horizontally, and vertically
    android:text="You place your text here..." // Text inside the TextView
    android:background="background Here"/> // Background (drawable or color)
Run Code Online (Sandbox Code Playgroud)