【问题标题】:Android: How to display transparent loading layer above the activityAndroid:如何在活动上方显示透明加载层
【发布时间】:2019-07-30 08:00:00
【问题描述】:

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

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

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

但是这两种方法都隐藏了活动,我希望像下图一样半透明,我该怎么做?

【问题讨论】:

  • 为此创建一个自定义对话框。这样它就可以重复使用,您不必担心点击后退按钮。

标签: android animation progress-bar preloading


【解决方案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>

【讨论】:

  • 不需要创建任何新东西,只需像这样制作
  • 在此解决方案中,背景属性显示为黑色纯色,使用 ARGB 格式而不是 RGB。类似#77000000
  • 为什么要使用 argb?这里
  • 用alpha显示半透明层,否则进度层将是不透明的
  • 请检查知道,知道我认为,可能会解决您的问题
【解决方案2】:

稍后谢谢我

  1. 创建样式LoadingDialogTheme

    <style name="LoadingDialogTheme" parent="Widget.AppCompat.ProgressBar">
        <item name="background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>
    
  2. 创建颜色:

<color name="dark_overlay">#B3000000</color>
  1. 创建布局layout_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@color/dark_overlay"
       android:id="@+id/loading_container">
   
       <ProgressBar
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"/>
   
   </RelativeLayout>
  1. 扩展对话框LoadingDialog
    open class LoadingDialog(context: Context) : Dialog(context, R.style.LoadingDialogTheme) {
        private val mContext: Context = context
    
         override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            requestWindowFeature(Window.FEATURE_NO_TITLE)
            val inflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val inflateView: View = inflater.inflate(R.layout.loading_dialog, findViewById(R.id.loading_container))
             setCancelable(false)
            setContentView(inflateView)
        }
    
    }
  1. 使用它
val loader = LoadingDialog(requireContext()) // <-- context
loader.show()

【讨论】:

    【解决方案3】:

    抱歉,我忘记添加课程了。

    你可以试试这个,

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

    【讨论】:

    • 请问是什么风格?警报对话框自定义
    • 没必要,误删了。问候。
    猜你喜欢
    • 2023-01-27
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多