簡體   English   中英

單擊按鈕后無法顯示進度欄

[英]Can't make progress bar visible after button click

我想在單擊按鈕后顯示簡單的圓形進度條,但這是行不通的。 看我的XML

            <FrameLayout
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:visibility="visible"
                 android:layout_centerHorizontal="true"
                 android:layout_centerVertical="true"
                 android:layout_gravity="center"
                 android:gravity="center"
                 android:id="@+id/progress_layout">

            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar"
                android:layout_gravity="center"
                android:visibility="invisible" />
        </FrameLayout>

而我的Java代碼

postPet = (Button) myFragmentView.findViewById(R.id.post_pet);
    postPet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progressBar.setVisibility(View.VISIBLE);
        }
     });

沒有錯誤,單擊后無任何反應。

PS我在片段的onCreate方法中初始化變量progressBar

    LayoutInflater infl = getActivity().getLayoutInflater();
    ViewGroup rootGroup = (ViewGroup) getActivity().findViewById(R.id.progress_layout);
    View root = infl.inflate(R.layout.fragment_new_pet_form, rootGroup);
    progressBar = (ProgressBar) root.findViewById(R.id.progressBar);

您應該在xml中的按鈕上方添加pb,例如:

  <FrameLayout
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:visibility="visible"
             android:layout_centerHorizontal="true"
             android:layout_centerVertical="true"
             android:layout_gravity="center"
             android:gravity="center"
             android:id="@+id/progress_layout">
<Button
android:id="@+id/post_pet"
your button description here />
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/progressBar"
            android:layout_gravity="center"
            android:visibility="gone" />
    </FrameLayout>

這樣,它就可以正常工作,您無需額外充氣pb。

嘗試添加一種關聯的樣式: http : //developer.android.com/reference/android/widget/ProgressBar.html

您還可以將其父級的背景色設置為某個值,並查看任何部分是否不透明:如果不是,則進度條沒有寬度和高度或不可見。

最終,您可以將hierarchyviewer與仿真器一起使用以檢查布局並驗證正在發生的事情。

同樣,如果框架布局沒有其他子框架,則將其刪除,然后使用layout_gravity將進度欄放置在其父框架中:無需額外的視圖組!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM