简体   繁体   English

Android Studio中相对布局问题中Button上的进度条

[英]progressbar on top of Button in relative layout issue in Android Studio

Ok this is a weird one I hope someone can explain to me. 好的,这是一个奇怪的,我希望有人可以向我解释。

I have a custom button layout which creates a button with a circular progress bar in the middle of the button. 我有一个自定义按钮布局,在按钮中间创建一个带有圆形进度条的按钮。 My XML code is below. 我的XML代码如下。 What I can't work out however is that the ProgressBar seems to be appearing behind the button. 然而,我无法解决的问题是ProgressBar似乎出现在按钮后面。 If I set the button background to anything other than transparent the progressbar cannot be seen. 如果我将按钮背景设置为透明以外的任何其他内容,则无法看到进度条。 With the button background as transparent I can then see the ProgressBar but it still appears behind the button text. 按钮背景为透明,然后我可以看到ProgressBar但它仍然出现在按钮文本后面。 I was under the understanding that views appeared in the order they are added. 我的理解是视图按照添加的顺序出现。 I have even tried setting the view to be on top (view.bringToFront();) and I've tried removing the view and recreating it. 我甚至尝试将视图设置在顶部(view.bringToFront();)并且我尝试删除视图并重新创建它。

Why does the progressbar appear behind the button and what can I do to solve it? 为什么进度条出现在按钮后面,我该怎么做才能解决它?

Many thanks 非常感谢

<?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> 

Code using the above layout 代码使用上面的布局

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

    }

}

You are propably running this on android >= 5.0. 你可以在android> = 5.0上运行它。 In 5.0 they added elevation field for views. 在5.0中,他们为视图添加了高程字段。 Elevation defines z-order of views in ViewGroup. Elevation定义ViewGroup中的z视图顺序。

In that case button have non-zero elevation value and progress bar have zero value elevation. 在这种情况下,按钮具有非零高程值,进度条具有零值提升。

Set elevation of progress bar to eg 10dp 将进度条的高程设置为例如10dp

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

Put your button into another layout (best choice for this case is probably FrameLayout ). 将您的按钮放入另一个布局(此案例的最佳选择可能是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>

I can't tell you why exactly you get that effect, but I suppose that is a bug. 我无法告诉你为什么你会得到这种效果,但我想这是一个错误。 Notice that if you replace Button with other view, for example TextView that problem doesn't exits. 请注意,如果将Button替换为其他视图,例如TextView ,则问题不会退出。 But when you change RelativeLayout to any other (tested with FrameLayout ) this bug still appears. 但是,当您将RelativeLayout更改为任何其他(使用FrameLayout测试)时,仍会出现此错误。 I guess it's going about background property and order of drawing or measurement in any layout. 我想这是关于background属性以及任何布局中绘图或测量的顺序。

try using FrameLayout like this 尝试像这样使用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> 

See this link 看到这个链接

Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. 通常,FrameLayout应该用于保存单个子视图,因为很难以可扩展到不同屏幕大小而儿童不会相互重叠的方式组织子视图。 You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute. 但是,您可以使用android:layout_gravity属性将多个子项添加到FrameLayout并通过为每个子项分配重力来控制它们在FrameLayout中的位置。

Child views are drawn in a stack, with the most recently added child on top. 子视图以堆栈形式绘制,最近添加的子项位于顶部。

By adding marginTop you can do that.. otherwise you can change the structure of button and progress bar... 通过添加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>

this code is working fine for me :D 这段代码对我来说很好:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM