Why does bringToFront works only once

自闭症网瘾萝莉.ら 提交于 2019-12-22 10:44:29

问题


bringToFront works once and after that it no longer works. Why? when you click once it brings that view to front but when you try to bring back the view it no longer works . The touches/clicks work fine all the time.

    View view1,view2,view3,view4; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //sets the Bundle  

    //code to initiate and putting the views in layout 
    view1= findViewById(R.id.button1);
    view2= findViewById(R.id.button2);

    view1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Log.i("onClick","1");
            view1.bringToFront();
            view1.invalidate(); 
            view2.invalidate(); 
        }
    });     
    view2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Log.i("onClick","2");
            view2.bringToFront(); 
            view1.invalidate(); 
            view2.invalidate(); 
        }
    });
}       

this is the xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <TextView
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="30dp"
        android:text="Button2ButtonButton"
        android:textColor="#00ff00"
        android:textSize="60dp" />

    <TextView
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="170dp"
        android:text="Button1ButtonButton"
        android:textColor="#ff0000"
        android:textSize="60dp" />

</RelativeLayout>

回答1:


I think I solved. I had to put everything in a Layer and invalidate that as well.




回答2:


you have to put the views on the top of other to get this work..

like..

public class MainActivity extends Activity {

RelativeLayout rl1,rl2;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //rl1=(RelativeLayout)findViewById(R.id.rl1);
    //rl2=(RelativeLayout)findViewById(R.id.rl2);

    b1=(Button)findViewById(R.id.b1);
    b2=(Button)findViewById(R.id.b2);

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            b2.bringToFront();
            b1.invalidate();
            b2.invalidate();
            Toast.makeText(getApplicationContext(),"b1",1).show();
        }
    });
    b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            b1.bringToFront();
            b1.invalidate();
            b2.invalidate();
            Toast.makeText(getApplicationContext(),"b2",1).show();
        }
    });

}

and the xml is...

<RelativeLayout
    android:id="@+id/rl1" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >
    <Button 
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:layout_alignParentLeft="true"
        />

    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Button2" />

</RelativeLayout>

in which both button are one over another and when you click once then it will give the related toast and second click gives the second toast and so on...




回答3:


I found invalidate is enough for bring to front (API 17 not bringing sometimes, API 15 everytime ok)




回答4:


I have been able to sort this

if (android.os.Build.VERSION.SDK_INT >= 21) //Lollipop
{
    view.setZ(2);

    for (int i = 0; i < view.getParent().getChildCount(); i++)
    {
        if(view != view.getParent().getChildAt(i))
        {
            view.getParent().getChildAt(i).SetZ(1);
        }
    }
}

view.bringToFront();
for (int i = 0; i < view.getParent().getChildCount(); i++)
{
    view.getParent().getChildAt(i).invalidate();
}
view.getParent().requestLayout();
view.getParent().invalidate();


来源:https://stackoverflow.com/questions/16502800/why-does-bringtofront-works-only-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!