【问题标题】:Android Studio: Button always appears at the frontAndroid Studio:按钮总是出现在最前面
【发布时间】:2015-07-06 13:14:31
【问题描述】:

我有一个向其添加视图的 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);
}

【问题讨论】:

  • 可能其他视图没有背景?发布您的代码。
  • 也许一些相关的代码可能会有所帮助。
  • 在我的问题中添加了代码。

标签: java android android-studio


【解决方案1】:

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

loginButton.setEnabled(false);

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      从 Lollipop 开始,一个 StateListAnimator 控制高度被添加到默认的 Button 样式中。以我的经验,这会强制按钮出现在其他所有内容之上,而不管在 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 Works for View, but not Button?

      【讨论】:

        【解决方案4】:

        按钮似乎浮动到它所在的 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);
        }
        

        【讨论】:

          【解决方案5】:

          来自安卓开发者文档:

          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

          试试下面的 sn-p :

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

          【讨论】:

          • 问题是图层而不是放置,很抱歉,您的回答对我没有帮助。
          猜你喜欢
          • 2011-09-24
          • 2012-12-17
          • 2022-11-13
          • 1970-01-01
          • 2016-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-15
          相关资源
          最近更新 更多