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

时间:2015-04-27 21:55:37

标签: java android android-studio

我有一个RelativeLayout,我添加了Views。

我添加了一个按钮,按钮始终显示在添加到其中的所有其他视图的前面,而不管添加内容的顺序如何。怎么样?

我纯粹用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);
}

5 个答案:

答案 0 :(得分:18)

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

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

XML:

android:stateListAnimator="@null"

爪哇:

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 Works for View, but not Button?

答案 1 :(得分: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);
}

答案 2 :(得分: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);

答案 3 :(得分: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);
}

答案 4 :(得分:0)

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

loginButton.setEnabled(false);