gpt4 book ai didi

android - 在屏幕底部实现 Gmail Like "No connection"警报

转载 作者:行者123 更新时间:2023-11-29 21:18:24 24 4
gpt4 key购买 nike

下面是 Gmail 无连接警报。我将如何在我的应用程序中实现这一目标?

注意:这是UI实现的问题,不判断是否有连接。 enter image description here

编辑::

底部通知是以对话框为主题并放置在底部的 Activity ,还是某种 AlertDialog?

最佳答案

也许这个例子可以帮助你https://github.com/DanielRasta/Material-Design-Alert-Box-

编辑 2:Material Design有Snackbar也上课

编辑 1:

嗯,思路是调用前面的dialog activity。所以要这样做:

1 - 创建 alertbox_activity.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#8A8A8A">

<TextView
android:id="@+id/alertbox_message"
android:layout_width="match_parent"
android:paddingTop="10dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:background="#8A8A8A"
android:textSize="20sp"
android:typeface="sans"
android:gravity="center" />

</LinearLayout>

2 - 创建一个名为 AlertBoxActivity.java 的 Activity 并粘贴此代码

public class AlertBoxActivity extends Activity {
// ===========================================================
// Constants
// ===========================================================
private static final int EXIT_DELAY= 3500;
public static final String KEY_BUNDLE_MESSAGE_TEXT = "AlertBoxActivity::message_text";
public static final String KEY_BUNDLE_PARENT_WIDTH = "AlertBoxActivity::parent_width";
private static final String ERROR_GEN = "Oops.. Something went wrong.";

private Runnable Exit(){
return new Runnable(){
@Override
public void run() {
finish();
}
};
}

// ===========================================================
// Methods for/from super class
// ===========================================================
@Override
public void onCreate(Bundle pBundle) {
super.onCreate(pBundle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.alertbox_activity);
getWindow().setBackgroundDrawableResource(android.R.color.transparent);

TextView textViewMessage = (TextView) findViewById(R.id.alertbox_message);
String strNotificationToShow = ERROR_GEN;
int myParentWidth = getWindow().getAttributes().width;
try {
Bundle extras = getIntent().getExtras();
if(extras != null) {
strNotificationToShow = extras.get(KEY_BUNDLE_MESSAGE_TEXT).toString();
myParentWidth = extras.getInt(KEY_BUNDLE_PARENT_WIDTH);
}
else{
myParentWidth = getWindow().getAttributes().width;
}
}
catch(Exception e) {
e.printStackTrace();
strNotificationToShow = ERROR_GEN;
myParentWidth = getWindow().getAttributes().width;
}
finally{
textViewMessage.setText(strNotificationToShow);
}

WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = myParentWidth;
params.gravity = Gravity.BOTTOM;

getWindow().setAttributes(params);

Window window = getWindow();
window.setAttributes((android.view.WindowManager.LayoutParams) params);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

new Handler().postDelayed(Exit(), EXIT_DELAY);
}
}

3 - 将这些行添加到您的 manifest.xml

<activity
android:name="com.package.AlertBoxActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Dialog">
<intent-filter>
<action android:name="android.intent.action.ALERT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

4 - 调用它

Intent i = new Intent("android.intent.action.ALERT");       
i.putExtra(AlertBoxActivity.KEY_BUNDLE_MESSAGE_TEXT, "YOUR ALERT GOES HERE!");
i.putExtra(AlertBoxActivity.KEY_BUNDLE_PARENT_WIDTH, getWindow().getAttributes().width);
startActivity(i);

完成。

关于android - 在屏幕底部实现 Gmail Like "No connection"警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21018283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com