Android:如何在活动上方显示透明加载层

问题描述 投票:0回答:2

我测试了两种方法在活动上方显示透明加载层(进度条),但活动内容隐藏在这里是第一个:

<RelativeLayout
  android:id="@+id/loadingPanel"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center" >

  <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true" />
</RelativeLayout>

和风格的另一种方法

<RelativeLayout
  style="@style/GenericProgressBackground"
  android:id="@+id/loadingPanel">
  <ProgressBar
    style="@style/GenericProgressIndicator"/>
</RelativeLayout>

<style name="GenericProgressBackground" parent="android:Theme">
  <item name="android:layout_width">fill_parent</item>
  <item name="android:layout_height">fill_parent</item>
  <item name="android:background">#DD111111</item>
  <item name="android:gravity">center</item>
</style>
<style name="GenericProgressIndicator"  arent="@android:style/Widget.ProgressBar.Small">
  <item name="android:layout_width">wrap_content</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:indeterminate">true</item>
</style>

并隐藏或显示它

findViewById(R.id.loadingPanel).setVisibility(View.GONE);

(两者都作为根视图中的第一项添加)

但是这两种方法都隐藏了活动,我希望半透明可见,如下图所示,我该怎么做?

enter image description here

android animation progress-bar preloading
2个回答
1
投票

像这样,

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <!-- Your lay out code here-->

</LinearLayout>

 <RelativeLayout
    android:id="@+id/loadingPanel"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.8"
        android:background="#000000" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:indeterminate="true" />


</RelativeLayout>

</FrameLayout>

1
投票

对不起,我忘了加课。

你可以尝试这个,

public class LoadingDialog extends Dialog {
    private Context mContext;

    public LoadingDialog(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View inflateView = inflater.inflate(R.layout.loading_dialog, (ViewGroup) findViewById(R.id.loading_cont));
        setContentView(inflateView);
    }

}

添加此布局loading_dialog: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/loading_cont"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RelativeLayout
        android:id="@+id/loading_dialog_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00000000" >

        <ProgressBar
            android:id="@+id/login_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true" >
        </ProgressBar>

    </RelativeLayout>

</LinearLayout>

之后加入你的课程

LoadingDialog loadingDialog = new LoadingDialog(this);
loadingDialog.show();
© www.soinside.com 2019 - 2024. All rights reserved.