android - 在 imageview android 中间插入一个 textview

标签 android layout textview imageview

我需要在 ImageView 中间放置一个 TextView ,所以我有这段代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/menu_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lines="1" />

<ImageView
    android:id="@+id/menu_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerInParent="true"
    android:layout_marginBottom="@dimen/menu_icon_margin_top_bottom"
    android:layout_marginRight="@dimen/menu_icon_margin_right"
    android:layout_marginTop="@dimen/menu_icon_margin_top_bottom" />

<TextView
    android:id="@+id/notif_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/menu_icon"
    android:layout_alignLeft="@id/menu_icon"
    android:layout_alignRight="@id/menu_icon"
    android:layout_alignTop="@id/menu_icon"
    android:lines="1"
    android:text="69"
    android:textColor="@android:color/white" />

我需要最后一个 textview 始终位于 menu_icon ImageView 的中心,但我的方法不起作用,它只是将它放在左上角。

最佳答案

由于您的 ImageView 没有填充所有 RelativeLayout,您无法达到理想的效果。我建议您将 ImageView 和 TextView 放在 FrameLayout 中。

<FrameLayout
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerInParent="true">

 <TextView
    android:id="@+id/menu_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:lines="1" />

 <ImageView
    android:id="@+id/menu_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="@dimen/menu_icon_margin_top_bottom"
    android:layout_marginRight="@dimen/menu_icon_margin_right"
    android:layout_marginTop="@dimen/menu_icon_margin_top_bottom" />
</FrameLayout>

关于android - 在 imageview android 中间插入一个 textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14666106/

相关文章:

java - 为什么我的 JTable 取代了我的其他 JComponent

html - 如何使用 flexbox 在 div 中水平和垂直居中 div?

一个TextView中的Android多色

android - 如何在 Android 的 textview 边框上启用动画

android - 在警告对话框中显示 TextView

android - 如何使用计时器检查 Corona SDK 中是否发生事件

android - AppCompatActivity 中未触发 onBackPressed() 方法

android - 跨子 fragment/Activity 协调 float 操作按钮行为

java - Android Play 商店订阅计费不会触发任何操作

java - 底部 Jpanel 中的按钮