Android Studio:按钮始终显示在前面 [英] Android Studio: Button always appears at the front

查看:440
本文介绍了Android Studio:按钮始终显示在前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RelativeLayout,我添加了视图。



我添加了一个按钮,按钮总是出现在添加的所有其他视图的前面无论添加内容的顺序如何。怎么样?



我纯粹用Java编写代码,没有XML编码。



这是一个简单的例子,按钮将出现在文本前面,即使文本是最后添加的:

  protected void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState);

RelativeLayout layout = new RelativeLayout(this);

按钮按钮=新按钮(此);
TextView text = new TextView(this);

button.setText(Button);
text.setText(Text);

layout.addView(按钮);
layout.addView(text);

setContentView(layout);
}


解决方案

从Lollipop开始,StateListAnimator控制高程已添加到默认的Button样式。根据我的经验,这会强制按钮显示在其他所有内容上,而不管XML中的位置(或者在您的情况下是程序化添加)。这可能就是您所遇到的。



要解决此问题,您可以根据需要添加自定义StateListAnimator,或者如果不需要,只需将其设置为null。 / p>

XML:

  android:stateListAnimator =@ null

Java:

 按钮按钮=新按钮(此); 
button.setText(Button);
if(Build.VERSION.SDK_INT> = Build.VERSION_CODES.LOLLIPOP){
button.setStateListAnimator(null);
}

更多详情:
Android 5.0 android:提升适用于View,但不适用于Button?


I have a RelativeLayout to which I add Views.

I added a button to it, and the button always appears in front of all the other Views that are added to it, regardless of the order in which things were added. How come?

I'm coding purely in Java, no XML.

Here's a simple example, the button will appear here in front of the text, even though the text was added last:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    RelativeLayout layout = new RelativeLayout(this);

    Button button = new Button(this);
    TextView text = new TextView(this);

    button.setText("Button");
    text.setText("Text");

    layout.addView(button);
    layout.addView(text);

    setContentView(layout);
}

解决方案

Starting with Lollipop, a StateListAnimator controlling elevation was added to the default Button style. In my experience, this forces buttons to appear above everything else regardless of placement in XML (or programmatic addition in your case). That might be what you're experiencing.

To resolve this you can add a custom StateListAnimator if you need it or simply set it to null if you don't.

XML:

android:stateListAnimator="@null"

Java:

Button button = new Button(this);
button.setText("Button");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    button.setStateListAnimator(null);
}

More details: Android 5.0 android:elevation Works for View, but not Button?

这篇关于Android Studio:按钮始终显示在前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆