简体   繁体   English

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

[英]How to make a view as always on top?

I have a TextView and it is on an image.我有一个TextView ,它在一个图像上。 When I click the button, TextView 's background color will change, but image won't disappear.当我点击按钮时, TextView的背景颜色会改变,但图像不会消失。 For example:例如:

My TextView at the beginning:我的TextView开头:

在此处输入图片说明

When I click a button:当我点击一个按钮时:

在此处输入图片说明

If you want to do this using only one TextView then its may be not possible.如果您只想使用一个TextView来做到这一点,那么它可能是不可能的。 So, I will suggest you to do this using FrameLayout .所以,我建议你使用FrameLayout来做到这一点。 you can write your layout as below.你可以写你的布局如下。

<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>

When you want to change the color behind the TextView then change the ImageView background as below...当您想更改TextView后面的颜色时,请更改ImageView背景,如下所示...

ImageView mageView view = (ImageView) findViewById(R.id.image);
view.setBackgroundColor(Color.BLUE);

If it must be a TextView and an ImageView you could also wrap it within a FrameLayout (which is supposed to contain only one child).如果它必须是一个 TextView 和一个 ImageView,你也可以将它包装在一个 FrameLayout (它应该只包含一个孩子)中。 A FrameLayout will place all containing children elements on the same "place", so the get overlapped. FrameLayout 会将所有包含的子元素放置在同一个“位置”上,因此会重叠。

Or when you need more elements, you could also use a RelativeLayout and give your elements the same layout rules (eg all centered).或者当您需要更多元素时,您也可以使用 RelativeLayout 并为您的元素提供相同的布局规则(例如全部居中)。

You should use ImageView instead of TextView, the:您应该使用 ImageView 而不是 TextView,即:

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

ImageButton is better option for you.Image source will be star as u said.then onclick you change the background. ImageButton 对你来说是更好的选择。图像源将像你说的那样成为明星。然后点击你改变背景。 if want set Text in this如果要在此设置文本

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM