android - 如何保持 TextView 正方形?

标签 android layout view

我找到了 this answer ,它确保 ImageView 的宽高比得以保留。

我如何使用带有可绘制背景的 TextView 来做到这一点?我有这个 TextView:

       <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:padding="5dp"
            android:layout_margin="5dp"
            android:text="1"
            android:textColor="@color/white"
            android:id="@+id/tvCount"
            android:background="@drawable/textview_notify"
            android:layout_column="2"
            android:layout_gravity="right|top"/>

这是我的背景图:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <!-- The fill color -->
    <solid android:color="@color/red" />
    <!-- Just to add a border -->
    <stroke
        android:color="@color/white"
        android:width="2dp"
        />
</shape>

如何确保 TextView 保持正方形(因此背景是完美的圆形?)。

最佳答案

使用android:gravity="center"android:padding="@dimen/circle_radius。在dimens.xml中定义circle_radius维度文件。

关于android - 如何保持 TextView 正方形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35655281/

相关文章:

css - 为什么这不能正确对齐我的元素?

ios - 将 UIView 换入或换出 ViewController 的 View

sql - Oracle Material View 与分析工作区

android - 我应该总是先完成一项 Activity 再进行另一项 Activity 吗?

css - 在 View 上 react native 布局 ScrollView

android - 错误 : impossible to create a new session because 'createSession' which takes HttpClient, 未找到 InputStream 和 long

html - Bootstrap 3 推拉类在我的布局中创建了一个空白

java - 布局 XML 中添加的 EditText 与代码中添加的 EditText 之间存在奇怪的差异

android - 如何在其顶部创建具有许多可拖动 View 的可缩放和平移 View ?

java - 如何实现 PdfRenderer 缩放和滚动支持?