您的位置:首页 > 移动开发 > Android开发

Android半透明等待界面及使用线程控制结束

2016-01-09 20:51 363 查看
1.半透明等待界面xml

layout代码

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

<RelativeLayout
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:background="#2A2A2A" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载中..."
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>

</RelativeLayout>


设置style代码(半透明),写在res/values/styles.xml

<style name="MyDialogStyle">
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:windowFrame">@null</item>
<!-- 边框 -->
<item name="android:windowNoTitle">true</item>
<!-- 无标题 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 半透明 -->
<item name="android:windowContentOverlay">@null</item>
<!-- 内容覆盖 -->
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<!-- 窗口样式Dialog -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 模糊 -->

 </style>


然后在AndroidManifest.xml中相应Activity中添加配置,如

<activity android:name="LoadingActivity" android:theme="@style/MyDialogStyle"></activity>


2.使用线程来控制LoadingActivity结束

private Handler handler = new Handler() {

@Override
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
LoadingActivity.this.finish();
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.loading);

//可以设置在onCreate开始时运行线程
processThread();
}

private void processThread() {
new Thread(){
public void run() {
//这儿写需要运行的代码,在此代码运行结束收handler发送message
longTimeCode();
handler.sendEmptyMessage(0);
}
}.start();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: