Android Studio:按钮始终显示在前面

9
我有一个RelativeLayout,向其中添加视图。
我添加了一个按钮,并且该按钮总是出现在所有其他添加到其中的视图的前面,无论添加顺序如何。怎么回事?
我只使用Java编写代码,没有XML。
以下是一个简单的示例,即使文本最后添加,按钮也会在其前面显示:
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);
}

1
也许其他视图没有背景?请发布您的代码。 - Simas
2
也许一些相关的代码会有所帮助。 - AndroidEx
添加了代码到我的问题中。 - Maxim Laco
5个回答

27

从Lollipop开始,默认的Button样式中添加了一个控制高度的StateListAnimator。根据我的经验,这将强制按钮出现在XML中的任何其他位置之上(或者在您的情况下以编程方式添加)。这可能就是你所经历的。

要解决此问题,您可以添加自定义的StateListAnimator(如果需要),或者如果不需要,则将其设置为null。

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);
}

更多细节请参考:Android 5.0中android:elevation属性适用于View而不适用于Button?


1
在Android 5.0(API 21)及以上版本中,您必须将android:elevation添加到视图中。
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");
    button.setElevation(3.0f); // add this line, you could try with values > 3.0f

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

    setContentView(layout);
}

0

来自Android开发者文档:

By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.

http://developer.android.com/guide/topics/ui/layout/relative.html

尝试以下代码片段:
RelativeLayout layout = new RelativeLayout(this);

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

        button.setId(View.generateViewId());
        text.setId(View.generateViewId());
        button.setText("Button");
        text.setText("Text");

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW, text.getId());

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

问题在于图层而不是位置,很抱歉,您的答案对我没有帮助。 - Maxim Laco

0
似乎浮动到它所在的RelativeLayout的最前面,因此...

尝试这个:

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

    Button button = new Button(this);
    button.setText("Button");

    RelativeLayout groupContainingButton = new RelativeLayout(this);
    groupContainingButton.addView(button);

    TextView text = new TextView(this);
    text.setText("Text");

    RelativeLayout activityLayout = new RelativeLayout(this);
    activityLayout.addView(groupContainingButton);
    activityLayout.addView(text);

    setContentView(activityLayout);
}

0

检查按钮状态(启用/禁用):

loginButton.setEnabled(false);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接