【问题标题】:progressbar on top of Button in relative layout issue in Android StudioAndroid Studio中相对布局问题中按钮顶部的进度条
【发布时间】:2017-02-20 21:31:49
【问题描述】:

好的,这是一个奇怪的问题,我希望有人可以向我解释。

我有一个自定义按钮布局,它在按钮中间创建一个带有圆形进度条的按钮。我的 XML 代码如下。但是,我无法解决的是ProgressBar 似乎出现在按钮后面。如果我将按钮背景设置为透明以外的任何内容,则看不到进度条。随着按钮背景透明,我可以看到ProgressBar,但它仍然出现在按钮文本后面。我的理解是视图按添加顺序出现。我什至尝试将视图设置在顶部 (view.bringToFront();) 并且我尝试删除视图并重新创建它。

为什么进度条会出现在按钮后面,如何解决?

非常感谢

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@android:color/holo_blue_bright"
    android:padding="2dp">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:text="Button"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:singleLine="true"
        android:clickable="false">
    </Button>

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:layout_centerInParent="true"
        android:visibility="visible"
        />

</RelativeLayout> 

使用上述布局的代码

 private void setupTableLayout(int NumberOfRows, int NumberOfButtons){
    TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
    TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0, android.widget.TableRow.LayoutParams.MATCH_PARENT, 3f);
    TableLayout tableLayout = (TableLayout) findViewById(R.id.thetablelayout);
    tableLayout.removeAllViews();

    for (int i = 0; i < NumberOfRows; i++) {
        TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(tableParams);

        RelativeLayout btnOneLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);
        RelativeLayout btnTwoLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);

        ProgressBar btnOneProgressBar = (ProgressBar)btnOneLayout.findViewById(R.id.progressBar);
        ProgressBar btnTwoProgressBar = (ProgressBar)btnTwoLayout.findViewById(R.id.progressBar);

        btnOneLayout.setLayoutParams(rowParams);
        btnTwoLayout.setLayoutParams(rowParams);

        Button btnOne = (Button)btnOneLayout.findViewById(R.id.button);
        btnOne.setText("Btn 1, Row " + i);
        btnOne.setId(1001 + i);
        Button btnTwo = (Button)btnTwoLayout.findViewById(R.id.button);
        btnTwo.setText("Btn 2, Row " + i);
        btnTwo.setId(2001 + i);

        setButtonClickListener(btnOneLayout, btnOneProgressBar);
        setButtonLongClickListener(btnOneLayout, btnOneProgressBar);

        tableRow.addView(btnOneLayout); //Add layout, instead of just Button

        View adivider = new View(this);
        adivider.setLayoutParams(new TableRow.LayoutParams(20, TableRow.LayoutParams.MATCH_PARENT));
        adivider.setBackgroundColor(Color.TRANSPARENT);

        // This bit of code deals with odd/even numbers of buttons.
        if (((i + 1) * 2) < NumberOfButtons + 1) {
            tableRow.addView(adivider);
            tableRow.addView(btnTwoLayout);
        } else {
            tableRow.addView(adivider);

            btnTwoLayout.setBackgroundResource(android.R.color.transparent); 
            tableRow.addView(btnTwoLayout);
        }


        tableLayout.addView(tableRow);

    }

}

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    您很可能在 android >= 5.0 上运行它。在 5.0 中,他们为视图添加了高程字段。高度定义 ViewGroup 中视图的 z 顺序。

    在这种情况下,按钮的高度值非零,进度条的高度值为零。

    将进度条的高度设置为例如10dp

    <ProgressBar
        ...
        android:elevation="10dp"/>
    

    【讨论】:

    • 这不适用于 5.0 以下的 Android,这根本不是一个天才的答案。对于支持投票的人,您可能需要在评论和批准答案之前测试所有案例
    • 谷歌很烂,我爱你。
    【解决方案2】:

    将您的按钮放入另一个布局(这种情况下的最佳选择可能是FrameLayout)。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                ... >
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <Button
                android:id="@+id/button"
                ... />
    
        </FrameLayout>
    
        <ProgressBar
            android:id="@+id/progressBar"
            ... />
    
    </RelativeLayout>
    

    我不能告诉你为什么你会得到这种效果,但我想这是一个错误。请注意,如果您将Button 替换为其他视图,例如TextView,则不会出现该问题。但是当您将RelativeLayout 更改为任何其他(使用FrameLayout 测试)时,仍然会出现此错误。我想这与background 属性和任何布局中的绘图或测量顺序有关。

    【讨论】:

      【解决方案3】:

      尝试像这样使用FrameLayout

      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:background="@android:color/holo_blue_bright"
          android:padding="2dp">
      
          <Button
              android:id="@+id/button"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@android:color/transparent"
              android:text="Button"
              android:gravity="center"
              android:textColor="@android:color/white"
              android:singleLine="true"
              android:clickable="false">
          </Button>
      
          <ProgressBar
              android:layout_gravity="center"
              android:id="@+id/progressBar"
              android:layout_width="40dp"
              android:layout_height="40dp"
              android:background="@android:color/transparent"
              android:layout_centerInParent="true"
              android:visibility="visible"
              />
      
      </FrameLayout> 
      

      this link

      一般情况下,FrameLayout 应该用于容纳单个子视图, 因为很难以这样的方式组织子视图 可缩放到不同的屏幕尺寸而不会重叠 彼此。但是,您可以将多个子项添加到 FrameLayout 并通过分配重力来控制它们在 FrameLayout 中的位置 对每个孩子,使用 android:layout_gravity 属性。

      子视图在堆栈中绘制,最近添加的子视图在顶部。

      【讨论】:

      • 谢谢,但 pjanecze 的回答一针见血。它需要是一个相对布局,所以我不能接受你的回答。谢谢!!!!!!
      • 这是这句话后面的正确答案子视图被绘制在堆栈中,最近添加的子视图在顶部。,感谢您的注意。
      【解决方案4】:

      通过添加 marginTop 你可以做到这一点.. 否则你可以改变按钮和进度条的结构......

       <linearLayout android:orientation="horizontal" ... >
          <ImageView 
           android:id="@+id/thumbnail"
           android:layout_weight="0.8" 
           android:layout_width="0dip"
           android:layout_height="fill_parent"
          >
          </ImageView>
          <TextView 
          android:id="@+id/description"
          android:layout_marginTop="-20dip"
          android:layout_weight="0.2"
          android:layout_width="0dip"
          android:layout_height="wrap_content"
          >
          </TextView>
      

      这段代码对我来说很好用:D

      【讨论】:

      • 谢谢,但 pjanecze 的回答一针见血。它需要是一个相对布局,所以我不能接受你的回答。谢谢!!!!!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      相关资源
      最近更新 更多