![](/img/trans.png)
[英]Issue with layout in android studio. How do I get my relative layout to cover the entire screen and not stay stuck in the top left?
[英]progressbar on top of Button in relative layout issue in Android Studio
好的,這是一個奇怪的,我希望有人可以向我解釋。
我有一個自定義按鈕布局,在按鈕中間創建一個帶有圓形進度條的按鈕。 我的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);
}
}
你可以在android> = 5.0上運行它。 在5.0中,他們為視圖添加了高程字段。 Elevation定義ViewGroup中的z視圖順序。
在這種情況下,按鈕具有非零高程值,進度條具有零值提升。
將進度條的高程設置為例如10dp
<ProgressBar
...
android:elevation="10dp"/>
將您的按鈕放入另一個布局(此案例的最佳選擇可能是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
屬性以及任何布局中繪圖或測量的順序。
嘗試像這樣使用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>
看到這個鏈接
通常,FrameLayout應該用於保存單個子視圖,因為很難以可擴展到不同屏幕大小而兒童不會相互重疊的方式組織子視圖。 但是,您可以使用android:layout_gravity屬性將多個子項添加到FrameLayout並通過為每個子項分配重力來控制它們在FrameLayout中的位置。
子視圖以堆棧形式繪制,最近添加的子項位於頂部。
通過添加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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.