简体   繁体   中英

How to make a view as always on top?

I have a TextView and it is on an image. When I click the button, TextView 's background color will change, but image won't disappear. For example:

My TextView at the beginning:

在此处输入图片说明

When I click a button:

在此处输入图片说明

If you want to do this using only one TextView then its may be not possible. So, I will suggest you to do this using 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...

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). A FrameLayout will place all containing children elements on the same "place", so the get overlapped.

Or when you need more elements, you could also use a RelativeLayout and give your elements the same layout rules (eg all centered).

You should use ImageView instead of TextView, the:

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. if want set Text in this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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