Android 如何使视图始终位于顶部?

Android 如何使视图始终位于顶部?,android,always-on-top,android-background,Android,Always On Top,Android Background,我有一个文本视图,它位于图像上。当我点击按钮时,TextView的背景色会改变,但图像不会消失。例如: 我的TextView开头: 单击按钮时: 如果您只想使用一个TextView来执行此操作,则可能无法执行此操作。因此,我建议您使用FrameLayout执行此操作。你可以写你的布局如下 <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content">

我有一个
文本视图
,它位于图像上。当我点击按钮时,
TextView
的背景色会改变,但图像不会消失。例如:

我的
TextView
开头:

单击按钮时:


如果您只想使用一个
TextView
来执行此操作,则可能无法执行此操作。因此,我建议您使用
FrameLayout
执行此操作。你可以写你的布局如下

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</FrameLayout>

您应该使用ImageView而不是TextView:

public void onClick(View v){
  imageView.setBackgroundColor(Color.GREEN);
}

如果它必须是TextView和ImageView,您也可以将其包装在FrameLayout(应该只包含一个子项)中。FrameLayout会将所有包含子元素的元素放置在同一个“位置”,这样子元素就会重叠


或者,当您需要更多元素时,也可以使用RelativeLayout并为元素提供相同的布局规则(例如,全部居中)。

ImageButton更适合您。如您所说,图像源将为星形。单击后,您可以更改背景。如果你想在这里设置文本

android:drawableTop="@drawable/any_drawable"
android:text="@string/any_text"

但是我想动态地改变图像(星星)和背景色。我怎么做这个?谢谢。这对我很有用。我必须使用TextView:)没问题,代码相同,只是在我的ExMaple中将imageView更改为TextView,但这并不是我想要的。当我使用它时,图像(星星)将消失。不,开始不会消失,背景会回到图像
android:drawableTop="@drawable/any_drawable"
android:text="@string/any_text"